Your First iOS & SwiftUI App: An App from Scratch

Feb 13 2023 · Swift 5.7, iOS 16, Xcode 14

Part 2: SwiftUI Data

15. Strings

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 14. SwiftUI Bindings Next episode: 16. Variables

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

It's time to tackle the next item on our programming to-do list. Read the value of the slider after the user presses the Hit Me button. Before we start working on this, I want to tell you about a very important data type that you'll use in your iOS Apps Strings. To create a string in Swift, you simply surround some text with quotes. Behind the scenes, strings are just a sequence of characters. You can imagine them as a bunch of characters hanging onto a piece of string like you see here. Strings in Swift have a cool feature called string interpolation. That's a fancy way of saying that you can put placeholder values inside your string that are replaced dynamically by code when the app is running. Imagine you have a string where you want to put a dynamic value inside at runtime. For example, maybe you wanna say "Hello," and then the name of the user of the app. To do this, wherever you want the value to appear in your string, you put a backslash and two parenthesis and inside the parenthesis, you put some code that evaluates to the value to display. In this example, if the name is set to "Ozma" at runtime, the string will become "Hello, Ozma." Let's try this out by making our app print out the value of the slider inside a string. Okay, so we want to show the current value of the slider inside this alert message here. Instead of, "This is my first alert!" We want to say the slider's value is, and then put in the sliderValue. We can do that by using string interpolation and the way that works is, we put a backslash, two parentheses, and inside of the parentheses, we can put a reference to any code that evaluates to a value that can go in a string. In our case, we'll use sliderValue. This takes whatever the slider's current value is, and puts it into that string. I'm just going to make it a sentence that says "The slider's value is with a period at the end." Try it out right in the canvas, tap Hit me and that sets the slider's value to 50 points and a lot of zeros. That's great, but this level of precision is way more than we need. It would be nice to show this value as a whole number which is also known as an integer rather than having all of these decimal places. So how do we fix this? Well, you'll find that out in the next episode.