Swift 4 Strings

Aaron Caines
A free video tutorial from Aaron Caines
iOS 12, Swift 4.2, Objective-C & Xcode 10 Developer
4.4 instructor rating • 19 courses • 36,050 students

Lecture description

Learn how to create and use Strings in Swift 4.


Xcode 9

iOS 11

Swift 4

Objective-C


Learn more from the full course

iOS 11 and Xcode 9 - Complete Swift 4 & Objective-C Course

A Complete iOS 11 and Xcode 9 Course with Swift 4 & Objective-C

59:02:19 of on-demand video • Updated October 2017

  • Full understanding of Xcode 9 and all it has to offer
  • Learn how to program for IOS 11 to create your own apps
  • Create fully featured games
  • Be able to support all devices and screen sizes
  • Full explanation of all objects and functions within IOS 11 development
  • Develop in Objective-C and Swift 4
  • Earn revenue from your apps
  • Start a new career
English [Auto] Variables can come in many different forms. When you're creating your own applications and one of the most commonly used type of variable is a string. Now what is a string. A string is a type of variable that holds text in sign of it. So when you're only using stuff that labels this thing in text on the screen or kind of being conversant data over and you want to use a specific type of variable to control and use that data it's going to be a string. Now even playgrounds you already get one variable pre-created for you. And this happens to be a string. So we'll get rid of this to begin with and we may create our very own. So it creates a variable IPV which is short for variable then you can want to give that variable a name. So I'm gonna call it shreik an equal to a value. Now a string has to be formatted inside two quotation marks. That's why we place inside the two quotation marks where you can insert a variable is the text of what that variable is going to equal. Example I could type in the word hello. It means that if I go going call upon that variable at any given point if I choose to use that with my projets maybe I want to get the information inside that variable to be displayed on the screen. All I do is call up on my function. For example in playgrounds to call upon a function we do print and then call upon the string we created just up above. Now is the lowercase one. So I get the image was all tightly correct. Right. And you can see in a given area at the bottom here says hello I'm going to just put in my name. Hello Amran. Again when that gets printed the updates. So no matter what happens no I can always call upon the string which is going to call again text. Now any given point I can call up on the actual string itself that we've created an equal that to a new value. So maybe I don't say hello Ira maybe I get to say just an hour. So when I then go into print the string for a second time because the strings the value has not been updated before this got printed. The second print now it just says Amran. So you kind of it's very versatile a variable in constantly keep updates and change and we've learned previously before about variables and lets and how available again can be changed and the Letz can't be. So let's create a bunch of variables in that and let's apply them into something that we can go on to eventually use in our application. So let's just say you create an application and you want a bunch of variables that store a bunch of information. Example The first one could be the name of all user which the name of a use. Let's make this is called him Steve. And then the second information that we want to store is maybe something like his favorite food and we're equal that to Steve's favorite food. So let's say That's Steve's favorite food is pizza. We have these two variables. Now we have an application which is store in strings to information and we can distribute them at any given point in the application. And that's the core thing about creating these strings that already have predefined information. Any given time it's a print function here. And what is going to do is going to print a string so you can see here if I print it it's just going to say Hello Dan.. But look we all want to apply the information to hide these two variables inside of what I'm now creating. So how can your improvements unbearable into somewhere within your application. You need to do is type in the backslash here and do two brackets. That's called the short codes that place inside a variable. And what we get place inside is our name variable of our string. So when it goes on the print function. He now says Steve. So this whole section here the free Sambos and the name Voll variable at the top of our string links back to it. So after all I could do space like to eat space and then perform the same function but this time type in the food. Now what's cool about that is not only implements a variable inside of a function. But look I think it's formatted Deif likes to eat pizza. Maybe he came sign sight. Stephen go you know what. Actually I don't like pizza anymore. My favorite food is burgers. So when you change the variable Steve likes to eat burgers It means we haven't had to adjust the function. What so ever. We've changed the shrink we've been the variable which got updated in all the functions that food is called upon. That's the great thing about shrinks now because I predefined and preset of variable of all string to equal again a string. If you get to a INT's an error occurs and that's because once you've defined the variable to equal a string or anything else for that matter you can't really tell it to equal something else. So that's basically how we can use strings within our applications. They can store text in them and we can call upon in a given time and it's great but now the new version of X code for we are able to do something which I like to call a paragraph. Now what this allows us to do is format a bunch for large amounts of text in a way to create said paragraph. And this is really cool because before you would have to do a bunch of short code someone I mean by that is let's consecrates a new. So the Create variable here and I simply call it a paragraph and that's going to call a simple string. Now if I typed in a bunch of text for example. Hello my name is Avrin and if I wanted this to be on new lines I would have to do these really weird short codes like the backslash and the letter. And then what that would then go into do. If I go to print it print paragraph as you see the backslash and puts you on a new line and you can say hello. And then there is a new line that the short code for a new line is the backslash in the letter. And then I do one here by in the letter and you can see Hello my name is Aaron on free lines. Now while this does work when you're doing a vast amount of text it can get a little bit confused and a little bit scruffy when it comes down to how we implement all these shortcuts. Now the new impartation implementation of how we can format these paragraphs what we need to do is put in free quotation marks at the start and then free quotation marks. And now basically it's How's our string now. It's now become a multi-line string again which I like to call a paragraph. So how we format these I mean you can see straight away we've gotten a warning and how we do it is we add in our first free quotation marks on the first line then our string begins on the second line. And you want to align the text just after the last quotation mark ends. Then all we need to do then is add on each new line. But paste them all over them all lined up thinking this is a lot more work than most free. Well there's a shortcut that we added in and you could probably argue yes it is but the difference is we can now within our code see exactly how we would look like when he gets out puts it into our application and to finish it all off we do the exact same them for the free remained in quotation marks. But making sure that the first one is in line with the whole paragraph and the beginning of the end. So when that gets printed as you can see now nothing's really changed. Hello my name is Aaron. I got rid of resample put on a one line with an update. Hello my name is Aaron. So you could argue it's a little bit more work but if you create vast amounts of text on the screen and he needs add in for example about 20 of those short codes when you create an in a huge long line it goes across and across on a cross of text it looks a little bit confusing it's hard to see where you want to put the the short codes. It's hard to see the short codes within the text when you want to edit it this way you can format it within the code exactly how it would look like as you put it. That's pretty cool. You can create these no new formatted paragraphs. So we've learned how we can use these paragraphs as I want to call them and the strings. And we learn how we can create them and how we can use and how we can implement them into a function. And this is great but what about our real world project. How can we actually use these within X code when we create in our projects. Well what we're going to do now then is we take what we've learned here and implement it into a simple function within x. So already I have a project created here and it's a simple single view application that seemed to call it the strings. Now this application and the playground for that we've just created can be downloaded as a downloadable resource attached to dyslexia. So you want to download both of them play around them adjust them as you will. Go ahead do it. So what we're going to do is we can create a button and a label. Now when we press the button it's going to display text within the label but the text is going to be on display in is going to be coming from the variables of all strings that we've created. So let's jump into a up story storyboard and what we'll do is drag and drop in the bottom or space it out so we can clearly see it bring it down a bit. There we go. And then drag and drop in a label. So I drag and drop the label over and then centralize the text of the actually written spec so we can clearly see what gets placed inside of it. Now what needs to do is create an action and an outlet for these two objects to allow students to go on to trigger and display the text within it. So if you can afford Zona and bring up the assistant editor and we've been here and we start by space in the outlet section here said the label control click or right click and drag that over. I simply call it our label and can set that up at the bottom here. We're going to create an action for our buttons or drag and drop that over slightly as an action and I simply call it let's assume we're going to call it string. Action there. Spell it correctly and then connect that up. So we got to action. Let's not all created that's for me to do when these two views just fine. So I'm going to close this but go back to the standard editor and jump straight in to a view controller. SWIFT Well we just added in that coat. So just after that then we're going to create the variables for the strings that we want to use. So back again our playgrounds for that we've created if minimize that night you can see here we're going to create our variable of string here and get to a certain piece of text. We'll create two very similar to how we did the name for Steve and his burgers and then we'll print this line here into the label within assimilates so of bring our project back up them. So what we're going to do is create a variable called this name that's going to equal a string and it's going to be of course Steve Steve's back. He's in the application we create another variable I think and this way you implement variables in your application and it's going to be for food and it's going to go against a string. And again he still likes burgers. So let's keep burgers in we create two variables which are no strings so we can reference the name and the food variables we've in our project at any given point and display where we want. Now to do this what we do then is called upon our function we create time below. So when we press a button Well it's going to first do is call upon our label. So the label is the name of the outlet we created for the label give me give me the same name. It's one of the stunt. There are two dots and then select the text attribute of that label and then what we can do is equal the text attribute to a value. Now I can simply make it equal a string of simply saying hello and it would display the word hello within our label and that's simply how we can get it to display text. But I want to implement these two variables inside of it. So again just like how we've learned on the playground. We do our backslash two brackets in the name of the variable and then we can implement our own tax. And we have to implement a variable inside of it was the backslash and the brackets to backslash two brackets and we put in the name the variable that we created inside of our string. And every space likes to eat backslash brackets. There you go. And then the food. So again like I said before I can never never have to touch this function ever again. If I wanted the name of the food I'd do it within the variables the function remain the same. It's pretty cool right. So if I've referenced the food variable maybe 20 times for the code I don't have to go. Man you just did 20 times. I just did it once within the variable than 20 variables within the code then get updated. It's pretty cool right. So now I'm going to get a bolt and run and boot them by pressing the trunk at the top. They're now going to load up the simulator we can then test out the application. So just wait for this to load up and you can see once it's loaded up I didn't press my button which would trigger this action down below. And by pressing it. Steve likes to eat bugs. How cool is that. So that's how we can actually use strings in a real project. It's all you know it's pretty good that we can test it out on the playgrounds and play around with it but we can't really implement it in a real world application or real world situation. And this is how we can do it. So we're taking these two variables that we created as strings and implemented these strings within our project and created a function to display them. How cool is that. Now bowstrings are solely used to display text in the application. What about numbers. Can we do that. Well when it comes to numbers there's two ways in which we can do it. We can either add the numbering as a string. We want to use these numbers as a value to do calculations. We even do some stuff in the application and that's when we come down to using it. And you can use decimal points. That's when we come to use doubles. So in the next following as we go on you progress Frue this section of the course where we learn all the different things about variables and you know how we can learn more about the swift for language in the next lecture. We're going to focus on INS and doubles. We've been swift for.