Swift 4.2 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,788 students

Lecture description

Learn how to use strings within Swift 4.2.


Xcode 10

iOS 12

Swift 4.2

Objective-C

Learn more from the full course

iOS 12 & Xcode 10 - Complete Swift 4.2 & Objective-C Course

A Complete iOS 12 and Xcode 10 Course with Swift 4.2 & Objective-C

72:27:01 of on-demand video • Updated November 2018

  • Full understanding of Xcode 10 and all it has to offer
  • Learn how to program for IOS 12 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 12 development
  • Develop in Objective-C and Swift 4.2
  • Earn revenue from your apps
  • Start a new career
English [Auto] Now we're going to move on to talking about a specific type of variable when it comes to this language and that type of variable is known as a string now a string is what we use to display basic text on the screen or distributes text for the application. Now it creates a variable string. All you need to do is create the variable and you can get that type of value then automatically becomes a string. And we can use and distributes it for the application. Now we're going to do is learn about how we can create the strings how we can then update them and then how we can also implement other strings inside of a string. So it would confuse him to make sense as we progress along. And we went to talk about how we can go on to create power strings which are really really cool. Now I would him you create your very own playground file you automatically get generated a string variable within the project but as you can see right now. Now we're going to delay this. I mean we start from scratch at it. So to create a string variable. What is you typing. Which is short for variable. Then we go and say equal this or simply first name we give a name there we call it string and equal this. Now to the value of a string format now a string format comes into quotation marks so you need the first quotation mark to start the string and it takes 20 equal for example we're going to type in Hello. And then you end your quotation mark to end the string so that if we ever reference that string variable it's always going to turn the value of hello. Again it's a you know string format. So only some of us want to be Prynne. But since Hoyte of string up above not a personal play symbol know to run our little project you don't see when it all succeeds in the bottom then it says the word hello because that's what's turning in the string value. Find somebody my name for example and then run it this time and it turns out right. So it's when I call upon reference this string. Now it's not going to call upon the value inside. It's pretty cool. And at any point I'm going to update that string value. For example I can then reference the string and have that equal again a string. And I can say for example this point hello this one. So then if I print the new value string note is that the second one then it prints says hello. The first one says I run because that got printed before we updated it. It's pretty good isn't it. So why don't we take a little bit of a step further. So what we do now is create two variables that have two different types of strings inside of them to say two different things. The Mogen to combine them both to be printed in the same function. So we come to a place in external strings inside of a current string. That would be confusing but that makes sense. That's correct. The first variable here I would call it inane and call it some minor kind of a variable and call it food as if you can get to my favorite food. So let's say for example it's going to be chicken. Now we get so these two variables and I read the application and we want to use them in the app in some form of functionality. So this Princeville of functions so when this function here we're going to simply say I run likes to eat chicken. There we go. That's the function I want to perform but I want to get the iron and the chicken from the variables up above. So I'm using the function but when to take the kind of you know avows and so did these used to display and so on of them. So how do we implement these through place these here. Well we start with the errant first would delete it. So we're not using it and we need to place the short code to place a variable inside of a string which is the backslash and it's to write in brackets. Now inside that the two bind brackets be reference the variable that we want to use for example I type in nine and if I do the same for the chicken that I put in a backslash to write brackets in and type in food. I use the function that being printed. So it would be what it is in the name. Likes to eat whatever's in the food variable's. If I print that no you didn't see Aaron likes to eat chicken. How cool is that. Well the great thing about this is well you got to do them is update the variable so they will say you know what. Not really liking chicken today. I move a bug a guy. Aaron likes to eat bug as well it should say when I print it. And again I updated it from the variable and I don't have to touch the function below because it's dynamic. It calls upon it. Iran likes to eat burgers. It's pretty cool isn't it. So that's a kind of a basic overview of how we can create and use strings within our applications how we simply reference them update them and implement them into actual functions for our application. So now let's move on to a different type of string variable. Known as a paragraph and paragraphs are pretty cool. They were implemented in the last version of swift and that was the former. A vast amount of text in a paragraph form. So what kind of area the princess at the bottom Democrat end of available now call it paragraph. There we go. And now we're going to equal this to a vote. Now so no more string you pasting the first quotation mark. And the second one to end it when it comes to paragraphs is a little bit different. You have to create free quotation marks to start it and then free quotation marks to end it and I see what's happening in between the three of them then is simply what the paragraph is going to be formatted. So you want to do this on each line because a multi-line string as well is being referenced to hear what it said my name is Aaron I spelled my name right. And then we put the Friedland's at the end there. So what I like to do when Creighton is if you go to the first beginning and press the top button and type it over. So it's kind of all in orderly fashion. Now this is the person not to do it because that's your personal preference. It just makes a little bit easier to see in the code. So what do the free quotation marks the start the paragraph. And then we've got free lunch you have as many as you want it just not limited time. And you can have it says Hello my name is Aram and I end up on the final line after it with the free quotation marks. By then go into print paragraph. You can then see how it gets formatted. So I went up to that line and you can see this here. Hello my name is Aaron. Well I'm free lines. It's pretty cool. So it's a great way to distribute a large amount of tax. And what's good about it is you can sort it out and it's not and so you distribute them formats in a large piece of text becomes very very easy in swift. They've had this long line and you can see here Amerind likes the bug as if it's just lying on the screen. It will just go on and on and on. We don't have much screen space before we need to enter into a new light. Whereas this way we can format it to have you know an exact words that we one answer and one says take it further. Let's implement like when the verb was inside the paragraph. So hello my name is Erin and I like to eat. I mean did a backslash to rhyme brackets and place it off parts these over get this dynamic to come back to Ryan bracket sorry I didn't there go. So we feel like we've done up above and if I print it says Hello my name is Aaron and I like to eat bugs. It's pretty good isn't it. Suits like that. So we've invited quite a few things that we've learned quite a bit but there's nothing like really applying it to a real life scenario. So what do you do now in the news you know load up a project and actually apply this to something physical within an application the what would do rule creates what is known as a label on the screen which displays text and the text is going to display all comes down to what formats it from variables within our replicate so we would have to project kind of created right now. It's a simple single the application code it's strings and we're going to jump straight into the main that storyboard to begin with. We begin to add in a label to use that the output object to display our starting next to that little bit. It's up here and would simply say that the label drag and drop it in and I make it fairly big so we can kind of see on the screen that down a bit there are centralized attacks that make a little bit bigger. So we can clearly see it. And what we need to do is need to trade as an outlet so I can reference it and adjust the text we've been so good of a low side panel and bring up these two circles here for us just an editor and we've been here make sure you control us swift and after you if you can chose to print these bracket the press Enter a few times you get a room. This will be adding all of our outlets to control people right click on the label and drag and drop it over. Make sure the connection is an outlet and call it a label and press connect to create that connection. So I got a connection which I can then reference in the code so I'll close the season ended Sango about Swanston that is up. Let me go now if I go to boot and run for example on the iPhone X simulates. Now I want to get a feel for how the application looks before we just anything we've in it. So as it moves now it should display and it does the word label. That's what's in there by default. We haven't told it to update or display anything in particular. So let's do that then. So we didn't get to all of you can talk about Swift and I can see why we just implanted in our label. So now what are we going to do is create all the variables that we want to implement inside of the strength of the text. So if I go about you referencing what we did in the storyboard here you can see we create two variables the name and the food for the bugs. So if I copy those and we're going to implement them in side of our project. So if the outlet that is placed in the variables are two variables for our name and our food. Now we want to kind of format a string like Aaron likes to eat burgers. We've seen that like. I mean we just send the function up even the view did load function here. Now this function gets triggered when the view loads are proof in the application as soon as it loads up when he triggers it to display in our label. So what we're going to do then is going to reference the label. So we type in label which is the name we gave above and then we do dot to select an attribute of it and the B that I want to select is the text that JBU that's how we edited sex and change what we explain. So end of it. Now we going to equal that and we can get a simple string to begin with. So I can simply say hello if I Boothman the application the label will now update to say hello. That's simply how we use strings to display text and objects in a good way to say hello but I want to format it from the variables. So just like we did again if we go back to the playground for once more let's move over slightly. And you see here is pretty much the short code likes the short code. There we go. And that's exactly how we formants it. So it's open the backslash to rhyme bracket's the name space likes to its backslash two brackets. And the fruit. Simple as. So if I run we should have that nicely formatted string from a verbose now being displayed within our label of our application. And he says I run like that eat bogus. It's pretty cool isn't it. And then we get this and see how we can use and create verbal strings in our applications. Not only how we know how to create them how to update them how to implement them in strings hides paragraphs and even how to apply it to a real world scenario like displaying text on the screen. It's pretty cool isn't it. And then we go. That's how we use strings in the stryfe language.