Placeholder Image

字幕列表 影片播放

  • Hello.

  • Welcome to learn swift programming for beginner's lesson one.

  • Today we're gonna talk about variables.

  • You're gonna learn about what they are had to declare them and swift code, and furthermore, how to use them.

  • All right, so let's get started for the rest of the lessons in the Siri's, we're going to be working inside this app called X Code X Code is the tool that Apple has provided for people to build APS with and inside X code.

  • You're gonna be able to create your user interfaces.

  • You're gonna be able to write the code to express the logic in your app.

  • You're gonna be able to test your app.

  • Even so, that's why this is where we're going to start in learning Swift.

  • We're gonna be writing this swift code inside the app that you're actually owned to use to build ups with in the future.

  • So this is where you're gonna get X code.

  • Just go to your browser, go to developer dot apple dot com slash ex code.

  • Or you can launch the Mac app store on your computer and search for X code there.

  • If you're on the Web page, you can see that there's this big blue download button up top.

  • Now, Ideally, you want to make sure you're using the latest version of X Code, and right now, as of this recording, it's eight, and you can check your version if you go to X code.

  • Ah, you can see it here in the welcome screen.

  • And you can also go up here two x code about X code, and you can see what the version is.

  • If you are a PC user in your running windows, there may be some additional work you need to do before you can run X code.

  • Or you might have to use an alternative solution to build IOS ups.

  • So check in the description below for PC solutions.

  • However, if you are a PC user, don't let that discourage you.

  • Uh, for me.

  • Personally, I lost about 2 to 3 years of learning Iowa's developments simply because I didn't have a Mac and I didn't want to get a Mac.

  • But these days there are alternative solutions.

  • You don't actually have to buy a Mac computer, although I eventually did.

  • And now I haven't looked back s O.

  • Anyways, now that you have X code, and after you've installed it, it's a pretty large install.

  • It's pretty big program, so I might take some time.

  • But after you have it installed and you launch it, you're going to see this welcome screen.

  • You can see here that when you want to create an app for iPhone, iPad, Mac er, whatever you're going to go to create a new X Code project, and that's going to start a full out project for you to build a nap in.

  • But here, we're going to start a new playground because that's going to allow us to type some code quickly.

  • It's gonna let us run that code and see the results, and we're not committing to building in a project.

  • It's simply like a sandbox or a playground, as it's named.

  • Now, if you don't see this welcome screen, what you can do is just go up to file Neil and hit Playground right there, and that's going to start a new playground for you as well.

  • Or you could just hit this button, get started with the playground, so that's gonna launch this dialogue for you, and I'm simply going to leave the default name their platform should be Iowa's, and I'm going to save this on my desk cop.

  • Okay, so after your playground runs, you'll see up here, it's doing a whole bunch of stuff.

  • Ah, launching Simulator, getting it ready.

  • And when it's done all that, it's going to say, Ready?

  • You can already start typing in code.

  • So there you go.

  • It says, ready.

  • But when it doesn't say ready and it's doing something, then chances are whatever you're typing here is not going to show up on the right hand side.

  • Okay, we're gonna start with a quick tour of this playground window here.

  • So down on the left hand side, I have a bunch of line numbers.

  • Now, if you don't have these and you want them, they can be useful at times, especially when I'm teaching and I need to refer to line numbers.

  • You can go toe X code preferences, and you can go to text editing, and you can simply enable line numbers there.

  • Now, this area right here is where we're going to type our swift code, and it's already got a bunch of stuff for us right here, which we're going to go through in a second on the right hand side, you're going to see a quick preview of the results of what you type there.

  • Now, a lot of people have trouble with getting output to show up on this right hand side based on what they type here.

  • Um, well, one of the things that you can check is just in this status bar up here.

  • Make sure that it stays ready.

  • And as your typing something.

  • Let's say I change something like that.

  • You're going to see that it for a brief second there there was a little spinner because it's evaluating your code here.

  • Um, if you wanted to show up, just make sure it's as ready to process your next command.

  • And, you know, you should see that output there.

  • Alternatively, there is a trade down here which you may not notice because it's hidden.

  • At first, you can hit this button to show that the bug area, or there's also this little button here to hide the different panels.

  • Okay, but we don't need to see all of that, but this debug panel may be useful.

  • Ah, when we start to output things here, this is called a console area where we can use swift commands to output things.

  • This blue icon here is to execute the code in your playground.

  • So if you can't get things to show up on the right here, try clicking this button to run your playground.

  • Right now it's blue because you can see there's a little drop down arrow there.

  • If you click and hold it, you can see whether you want to switch it to automatically run.

  • And that's going to run the playground whenever you type something, or you can set it to manually run and you can see now it's great out.

  • So if I type in some new code, it's not actually going to change or display what I have on the right hand side here.

  • Unless I click the play button to run it, so I would recommend to have it set toe automatically run.

  • So now that you know the main areas of the playground, let's get into the meat of today's topic variables.

  • And in fact, what we have here is a variable in swift, a variable hold some data.

  • Now this data could be a value.

  • It could be a reference to an object, or it can even point to a function which are things that will get into the future.

  • But for now, we're going to say that variables hold data.

  • Now, as you can see from this example, line of code a variable has a couple of distinct parts.

  • Let's break it down.

  • Variables need to be declared before they can be used.

  • So this var key word is used to declare a new variable following the var keyword.

  • You have the variable name in this case, it's S t r.

  • And really that's it.

  • You've declared your new variable.

  • So it's got the var keyword and it's gotten to variable name now taking a look at the whole line.

  • What we have on the right hand side there, that's some data.

  • We did say that variables hold data right in that equal sign in between the variable name and the piece of data.

  • Well, that's an assignment operator.

  • So what we're doing in this line of code is we're declaring a variable called str and then a signing that piece of data to that variable.

  • So now jumping back into the playground, let's see the effect of that I can say something like print, which is a key word that is going to print something to that console area down below.

  • So if I say print and then in the parentheses I put str you can see that it's printed that hello?

  • Play out s rough lying down there on the right hand side.

  • We can see a quick preview of that and notice that there's a slash end here.

  • And that is basically, um, just denoting that there's a new line associated with that print statement because, ah, the next time I print something, it's gonna be printed on the next line.

  • So you don't see that here because a new line is just, um, a carriage return or white space.

  • You can't really see it, but in here in this preview, it's visualized as that slash and which is just a new line.

  • So let's take a look at a couple of different pieces of data that we can store into variables.

  • So I'm going to declare another variable here called a, and I'm going to assign a number into it.

  • So just bar one and I mean it clear another one called B, which I'm also a sign one, and I want to show you that you can perform operations between the variables or on the variables.

  • So, for example, I can save print A plus B, and you can see here that the result in the consul here is to now notice that what I'm using the variables in an equation or an operation like this I don't need to use the key word far, because that's only for declaring variables, right?

  • So this little part is declaring the variable, but when you use them afterwards, you just need to refer to them by name.

  • So let's try some other things.

  • Print a minus B imprint.

  • A times be okay, so variables hold data and you can perform operations on variables.

  • Now you can change the data that the variable is holding so I can say something like a equals two now, right?

  • In fact, actually, let's not do it down here.

  • Let's do it here after we've assigned one into a let's say, a equals two, and as a result, you can see all of these operations have changed because a is now too.

  • So a plus B is three a minus.

  • B is one and so on because A is now to now when you assigned something into a a different piece of data, it replaces what it was holding onto before so is no longer one.

  • It is now too.

  • But notice how when we assigned a new piece of data into a we don't have to write bar again.

  • That would cause an error because we've already declared that variable up here and memory in the system has been allocated to it.

  • And so we don't need to re declare it.

  • All we need to do is a sign new data into a like that.

  • Now, if you're playing around in the playground yourself, you may notice something like what?

  • If you try to assign something into str like one?

  • Well, you're gonna get an error and that's because of something called data types which we're going to learn about in the next lesson.

  • But for now, just know that for str because we've assigned some text into it up there.

  • If we want to reassign some data into it again, it has to be text us well, so that would be valid.

  • Furthermore, you can also a sign variable as another variable so I can say a equals B down here and in that case, a wood now equal one again.

  • Well, it was one to begin with, So So let's say I did that.

  • So if it was too and then I said a equals B, then now they would both be one.

  • Now, before we move on a quick note on naming variables Here we have a variable str and I've named some variables A and B so normal you want name variables like A and B because they're not descriptive of the data that they hold.

  • So if you're going to be using variables just like this in an operation, when it be easier if you knew what a represented in one be represented while you can declare your variables using any sorts of names that you'd want but you can't put some special characters and you can't put spaces in your variable name.

  • However, the common practice is to use a form of camel case where the first letter is lower case.

  • So let me show you what I mean, so I can save our And if I want to describe my valuable if it represents, um let's say the number of apples that I have.

  • I would say something like my number of apples like that, and you can see that each new letter of each word is capitalized except the 1st 1 Now you don't want to get too crazy with the length of the variable name as well.

  • And you don't want to go too short, so short that it doesn't actually mean anything.

  • So there's no right or wrong answer or hard limits.

  • It's based on, you know, your own preference.

  • And it's more important if you're working in a team with other people that you agree on a certain coding style because you're both going to be working on the same code.

  • Okay, so one more thing.

  • I want to talk about our Constance so they are like variables, except that once you assigned something to them, you can't reassign data into them.

  • So that's really the only difference.

  • That and also how they're declared.

  • So with variables, use the keyword var.

  • But with Constance, you use the key word let so I can say let C equals 10 and then down here.

  • If I try to reassign something in to see like that.

  • X code is not gonna like that.

  • It's gonna throw in error, and it's gonna tell you with this red dot down here, and if you click it, it's going to say cannot assign value to see because it's a let constant.

  • Now it gives you this little bubble here and what you can click and smartly, it's gonna change.

  • Let Tobar because it senses that you want to reassign data into it.

  • So you might ask, Why would I ever want to use a constant if it's more limited in its functionality, Right.

  • If I declare everything is variables, then I have all the flexibility I had one.

  • While there are a couple of reasons why you'd want to use constants when it makes sense.

  • For one thing, it helps the computer work a little more efficiently because it knows exactly what that constant will be.

  • And that's not gonna change.

  • But more importantly, it's that for you as a coder, you know that that constant isn't going to change, because when you're building your up, you're gonna have a lot of variables.

  • And when you have some sort of issue in your app and you don't know why it's happening in your tracking down your code or tracing it line by line, and you're trying to see if maybe at some point you've accidentally assigned something to a variable, and that's throwing off.

  • All your calculations are all over your logic.

  • It's going to be a nightmare to try to solve an untangle.

  • So if something you know it isn't gonna change, you can assign it to a constant.

  • And then when you're looking through your code, you can be sure that the issue doesn't lie with that constant because you know what can't change.

  • Okay, so that's it for this lesson on variables.

  • I hope you're practicing on your own computer with X Code and check below this video for a link to the cheat sheet and the notes.

  • Hello, Welcome to learns with for beginner's Lesson two.

  • And this lesson, we're going to talk about data types.

  • Did you know in the previous lesson that you're already working with different data types?

  • Well, let me point out to you let me open up the playground that we were working with in the previous lesson.

  • Do you remember how we were trying to reassign data into the variable.

  • Str and I was assigning something like a number into str and I told you that you couldn't do that.

  • In fact, Exco tells you that you can't do that right here.

  • So if we take this little console area and we scroll the scroll bar all the way up to the top, the error message actually tells you what's going on.

  • Cannot assign value of type end to type string.

  • Well, what's an int and what's a string?

  • While those air two examples of data types just as the name implies, a data type is a classification or a type of data.

  • For example, a string is a data type that describe pieces of text.

  • An end short for integer is a data type for whole numbers, negative or positive.

  • However, there are many more data types than just these two other types of data that you're commonly be working with our float for floating point numbers or decimal numbers.

  • And then there's double for decimal numbers where the decimal portion may be very large.

  • And then there's Boolean, which represents true or false or in other words, yes or no.

  • These are some of the more common data types that you'll be using.

  • And the reason why there are different types of data is because the system stores different types of data differently.

  • And so when we go back to the playground and we take a look at this variable that we declared her str and its stores a string, the system has allocated this variable to store this piece of text or string.

  • In other words.

  • And when you try to assign an int into that same variable, it doesn't allow you to do that because strings and ends are stored differently.

  • Now you might be wondering what differentiates A very well that stores a string like this.

  • Str variable versus a variable that stores a number or an end like this variable, eh?

  • Well, when you declare a variable, you learned in the previous lesson that you used to cured far and then the name of the variable Well, optionally.

  • You can also put beside the very well name colon followed by the data type.

  • And if you declare your variable this way, you're basically telling the system that this variable can only store this type of data.

  • And if you omit that part and you just simply declare your variable with far and then the variable name.

  • Then what it's gonna do is as soon as you assign a piece of data into that variable, it's going to take a look at what type of data that is and assumes that that variable stores that data type.

  • So now, going back into the playground here, it's as if we declared this s t r wearable like this, right?

  • And these ones are like this now, for instance, if I declared this str variable like this, then this line would be an error because I'm trying to assign a string into a variable that is of type int, So it's not going to like that.

  • Now let's change this back to a string for a second and go back to this line where there's an error.

  • Now you understand why this line is in red because we can't assign an inn into a string variable.

  • While there are ways to convert data from type to type, in some cases it makes sense, like 29 here.

  • This can be represented as a piece of text simply like that, right?

  • But conversely, this Hello playground line right here.

  • I can't convert that to an integer because it doesn't make sense that this message could be represented by a number.

  • So, for example, just that is a little preview here.

  • If I wanted to convert this number or this integer into a string, I would create any string and pass in the number like that.

  • Now, I'm not gonna go into detail about why this works or what this line is because then you're gonna have to learn about classes and initialize er's and stuff like that.

  • But for now, in this lesson, I want you to understand that there are different data types, why they're different data types and how that comes into effect when you're declaring your variables and working with your variables in your data.

  • Now, before we end this lesson off, let me just show you a couple of examples of the other data types we talked about.

  • So float could be something like that.

  • A double usually also looks like this, and you're not gonna have a really large decimal point unless it's like a result of a calculation that you're doing.

  • Um, and let me show you a Boolean which is represented as bull.

  • So you can set this to the keyword true or false, which is going to come in handy for the next lesson that we're going to do on if statements.

  • Oh, and there is a mistake here because I'm reading Claire ing.

  • See?

  • Did you see that in the previous lesson?

  • We talked about how you can't re declare variable C We declared Farsi up here, but I forgot that we had tried to declare a constant with the same name down here.

  • So this is gonna have to be f she some other types of data conversions here.

  • So I can say, Let's print out, um, this change see which is a float into an ent, And what's gonna happen is that it just drops the decimal portion of that.

  • So if you did print, let's convert D, which is a double into an integer can see here that it just chopped off the 0.9 and you get 13.

  • Now there is a rounding function, which we can try out right now like that and then inside the parentheses, you can put D, which is our double 13.9 and what you're going to get as soon as it finishes.

  • Processing is 14 like that.

  • But notice that the result is also a decimal number.

  • See, it has a 140.0 there.

  • So what you can do is wrap the result of that rounding inside a pair of brackets like this inside an int on.

  • Then you'll get 14 without the decimal.

  • Okay, so that does it for data types.

  • Hello.

  • Welcome to learn Swift for beginner's lesson three.

  • In this lesson, you're going to learn how to express some simple decision making in your swift code, using if statements.

  • All right, let's get started.

  • So for this one, we're going to start a brand new playground.

  • All right, so we've done this before.

  • Let's just call this one the if playground and I'm going to store this on my desktop.

  • Okay, so we're talking about if statements today and this is really exciting because it lets us finally start to make decisions through code and expressed some sort of logic.

  • So, for example, if I have, let's declare a constant here so that a equals 10 for example, and I wanted to print this statement, um, on Lee, if a is less than, let's say 11.

  • Which it is right here.

  • But mmm.

  • Okay, actually, that is That is not right.

  • Let's say four.

  • And this gives us a reason to use the F statement.

  • Right?

  • Okay.

  • So print only if a is less than four, but is 10 right?

  • So we shouldn't be out putting this so we can use here is an if statement and what it allows us to do is on Lee execute some code if a certain condition is true.

  • So in this case, we want to print this on Lee.

  • If a is less than four, so we can write an if statement to check if that's true or not, before we print that statement.

  • So let me show you what that syntax looks like.

  • And if you're not familiar with programming terms, syntax is simply like the grammatical structure of the language.

  • So it's basically the key words to use.

  • And, um, how we go about declaring an if statement or writing an if statement.

  • So it all starts with the if keyword following the If keyword, we have the condition in which we want to check and following the condition we have a set of curly brackets now inside the curly brackets.

  • That's where we put the code that we want to run.

  • If that condition is true, so that's your very basic.

  • If statement now going back to our playground, let's write it out and print this on Lee.

  • If a is less than four so we can do something like that if a and then we can use this operator less than four and remember, we need to surround the piece of code that we want to execute.

  • If the condition is true, using a pair of curly brackets like that so you can put the curly bracket on the same line or you can put it on a second line, it doesn't matter.

  • But what you usually want to do is in dent the code that is inside it just so it's easier to read.

  • I usually like to put this curly bracket in the same line like that.

  • Okay, so now you can see that in the console.

  • There's nothing printed, and also to the right here, it doesn't give you a preview because this condition is false.

  • A is not less than four, because is 10 now what if we changed a to one and let the playground process Okay.

  • Now it prints this statement and previews this statement.

  • See, it's printed down here.

  • So that's your very basic.

  • Um, if statement now, there are other cool things that you can do with an if statement there is an else if Klaus So, for example, using the else If Klaus, you can check a second condition If the first condition evaluates to false and the syntax for that looks like that.

  • So you have your if condition, and then you have your curly brackets and then you use the key words else if and then you check another condition and you have another set of curly brackets.

  • So if you write it this way, you're basically checking condition number one, you know, Is that true?

  • If it's false now, you're checking condition, too.

  • And if that actually evaluates the true, then you're running the code inside that else.

  • If block Now, keep in mind that it kind of goes from top down.

  • And if the first condition is actually true, then it's gonna run that piece of code inside the first set of curly brackets.

  • And it's just going to ignore your else if statement.

  • So let's take a look at that inside the playground.

  • So here, I'm gonna put else if a is less than eight, then now I'm going to print on Lee.

  • If a is less than eight so that I'm gonna change, just not this.

  • I'm gonna change, eh?

  • Back to 10 here and you can see nothing gets printed because first, the checks this condition.

  • If it's false, which it is, then it's gonna check this condition.

  • And it's also false so nothing gets printed.

  • Now, what if I changed a 27?

  • If I changed 8 to 7, you can see that it prints the second statement because first it checks this condition.

  • That's false.

  • So now it checks this condition, and that actually turned out to be true.

  • So it's going to run this line of code here.

  • However, if a is one, then it checks this 1st 1 and then it prints this one and then it totally ignores all of the other else.

  • If conditions Now, I said all of the other else, if conditions right.

  • I say that because you can have as many else if conditions as you want.

  • You can say if a less is less than 10 and then you can you can continue adding as many conditions as you want.

  • But keep in mind that it checks from top to bottom, and as soon as it finds a condition that is true, it's going to run that piece of code and ignore all of the other conditions.

  • Now, finally, there is a clause that you can run if all of the conditions are true, kind of like as a fail safe, where as a last resort and that is the else keyword.

  • So the way you write this is using the else cured.

  • It's not else if it's just simply else and there's no condition attached, because if all of the conditions above it are false, then it's going to run the code inside of this else statement here.

  • So, going back to the playground, let me show you what that looks like.

  • Um, else print.

  • Nothing was true.

  • And now let me change a back to 10 here so you can see that it prints nothing was true because it's checking the statement.

  • It's false.

  • The statement is false and sorry.

  • I mean conditions.

  • And this condition is also false because A is not less than 10 is actually 10.

  • So this would actually evaluate to false.

  • And finally, it just gets to this else Claus.

  • And it's going to print what's in here.

  • Okay, so let me show you a couple of other things that you can do with El statements.

  • And let's change these prints statements to something that is a lot more recognizable or easy to read.

  • Let's do that.

  • Branch one French too.

  • And here Oops.

  • Branch three.

  • Okay.

  • So, um, first of all, let me show you how to do less than or equal to.

  • Now you see it, Prince branch three.

  • So that's how you do.

  • Less than or equal to.

  • And likewise, you can do greater than or equal to, and you can see here.

  • It's still Prince Branch three.

  • Now, what if you wanted to check if it was exactly 10?

  • You wouldn't do that.

  • Because remember, this equal sign is an assignment operator.

  • So what you need to do to compare if a is equal to 10 used the double equal sign like that?

  • You can see here Prince Branch three.

  • Okay, So why don't we introduce another constant up here?

  • Let's say B and let's have that equal to four.

  • I want to show you that your conditions for your if statements and you're different branches can get pretty complicated.

  • And you can involve multiple pieces of data in your condition so you can go If a is less than four.

  • If you want to check, be as well and you want to say, you know end B is less than four.

  • That's how you would do it.

  • You would use this double ampersand sign, and now you're checking two conditions you're checking is a less than four end be less than four and only if both of those are true.

  • Are you going to get this branch here?

  • So let's try that out.

  • So if I said be to one and a tow one, then both of these conditions here are true, so it's gonna print Branch one.

  • However, if I set B 2 10 right, it's going to just see it hits this branch to now because A is one which is less than eight.

  • And it didn't print Branch one because this was false, right?

  • Even though a is one and this part is true.

  • We have to have this part to be true as well.

  • Because were you saying is a less than four end be less than four and B is 10.

  • Right now, however you can also do, or so you can say is a less than four or be less than four.

  • And in this case, either condition can be true and that would cause it to go into this branch.

  • So you can see now it is in Branch One.

  • All right, so let's say a is 10 NBS one, so a is not less than four.

  • But B is right because you're using this or statement you're saying is a less than four or be less than four.

  • And if that's true, then we're coming into here now.

  • Furthermore, if you wanted to involve, let's do a C.

  • You can combine.

  • You know, multiple conditions like this.

  • You can put this into compare of parentheses like this, and you can say, you know, end C is equal tow three.

  • So you can see now that you're saying okay is a less than four or B is less than four and also is C equal to three, then come into Branch One.

  • Now, these brackets matter a lot because what you're doing is you're saying that, um, this has to be true, and this has to be true.

  • But for example, if I just shift the brackets a little bit and I say something like this, then that changes the meaning completely.

  • Because now I'm saying is a less than four or is this part true?

  • Right.

  • So where your brackets are, matter a lot, and it changes the meaning of what you're checking.

  • Did you get that?

  • In this case, I am checking.

  • Is this true, or is this true?

  • But the other way around?

  • I'm checking.

  • Is this true?

  • End?

  • Is this true?

  • All right, so the brackets matter.

  • Finally, let me show you another operator that we have here.

  • So here we have we're checking is C equal to three.

  • Well, what if we want to ask is see anything but three or is c not equal to three?

  • If we only care about the case where C is not three, we can say something like this not equals 23 And this is going to say, um is C not equal to three.

  • So you can see here that sea is three.

  • So that's why it it completely skips this branch, and it ends up being a is equal to 10 and it prints branch three.

  • Now, you can use this, not operator.

  • This exclamation mark here on other things as well.

  • So, for example, here I'm asking, is a equal to 10.

  • Right?

  • And I am its coming in here because a actually is 10.

  • So it's printing branch three.

  • But if I surround this, um, like that, uh, let me put something else is in here is a equal to 10 and b equals toe one, right?

  • That is true.

  • So that's why I'm still getting branch three.

  • But if I put an exclamation mark here like that, it basically flips it around.

  • So this evaluates the true right is a 10.

  • This evaluates to true.

  • Both this whole condition evaluates the true right, um, eight is a equal to 10 end b equals one, and then so we get true.

  • But then we're adding this guy here, it flips that true to a false.

  • So that's why um, this whole condition equates to false.

  • Okay, so that does it for if statements.

  • I hope you're trying this out on your own computer in the playground because trust me, it's pointless to try to memorize all of the key words and the syntax for the swift.

  • It's much better if you, you know, only spend 30 minutes instead of trying to memorize things.

  • Spend 30 minutes in the playground.

  • Uh, just punching in different numbers like that, playing around with the different conditions and expressions on printing out a bunch of stuff in the console in 30 minutes.

  • You'll remember Ah, lot more just by doing that than trying to memorize you know how Do declare variable or how to declare an if statement.

  • So you know, if you can get a Mac, get X code open and a playground, and then type this code out for yourself and play around with it, it's going to do wonders.

  • So thanks again for watching.

  • And if you like the series so far, please give the video thumbs up.

  • Please subscribe.

  • It really helps.

  • Hello, Welcome to the learns with for beginners.

  • Siri's lesson for in this lesson, you're going to learn about switch statements and like that.

  • If statement it allows you to make decisions with your code.

  • All right, so let's get started.

  • Let's start by creating a new playground for this example, and I'm going to start labeling thes after the lessons here, and I should have done this sooner.

  • But I want to point out that Apple does have a swift programming language guide and in this guy that goes through all of the syntax and the structure in the different concepts of the swift programming language.

  • It's what we're doing in thes videos and Mawr, in fact, so if you want something to read, you can go ahead and visit this website here.

  • I'll have the link below the video.

  • But one thing to know it is that it made be a little bit complicated for the absolute beginner who doesn't have programming background.

  • But give it a try, and it's a great compliment.

  • Tow watch with this learned swift Siri's that you're watching.

  • In fact, as we're going through the different concepts in these videos, I'll point out in the guide where they are.

  • So, for example, we're looking at the switch statement today and that resides under control flow, and you can see here What the basics in taxes for this switch statement.

  • Okay, so let's jump back into our playground and we can do an example of a switch statement.

  • So in here, I'm going to basically do the same example or similar example as the one in the guide.

  • And I'm going to introduce a new data type called Character.

  • And it's basically like a string, except that it's just a single character like that.

  • And with an if statement, we could do something like this if some character is, you know, equals a for example.

  • Okay, like we have right here, Print is in a and this is going to be true, and it's gonna print this out, and then we're gonna have else if and we're gonna maybe check another condition if we wanted to, you know, respond to different cases, depending on what letter?

  • This variable is right.

  • But you can use a switch statement and it looks like this.

  • So we start with the switch key word.

  • Then you have the value which you're considering in our case, that value is in the variable, some character.

  • So that's what we would put in there.

  • And then you open up a pair of curly brackets inside these curly brackets, You have different case labels.

  • So you would have case if you wanted to consider the case where the value is an A you would have case a and then you have colon and you'd have something to do If it were that case, perhaps this would be easier to visualize if we take a look at the playground.

  • So let's jump back to the playground here.

  • So let me say switch statement right on the value that we're considering.

  • So that's some character.

  • And you open up a pair of curly brackets like that, and then you have your case labels.

  • So I have a case.

  • You know, if it's an A and if I want to do something for this case, right, then you have a colon.

  • And in here, you would print, say, is an A like that.

  • And if there's another case right, I would have case be, for example, and I would say is a B.

  • And lastly, if none of those cases are true, I would have a default case like that and I would say, print some fall back.

  • So in this switch team in this case was true, right?

  • So it printed out this demon and it doesn't do the rest.

  • And you could have more lines of code in here than just the prints, Damon and all of our examples.

  • You know, I just wanted to show that it's coming in to hear this branch, but you could execute more code in here now the case where you know, character is a B.

  • Then we would come into here and jump into here.

  • Now, if it's not none of those cases, we would have default and it would print that.

  • And of course, you can have a whole bunch of different case labels.

  • It just doesn't have to be the two that we have here.

  • The benefit of using a switch statement is that rather than using an if statement and having all of these else if Klaus is reading, this is much more clean.

  • And it's a lot easier to read as well.

  • So what if you wanted, let's say, two different cases to run the same piece of code or the same block of coat.

  • So, for example, if I change this to say is be or you know s e is a b or C.

  • I could do something like this with this case label here.

  • I can do that comma.

  • And then I can put, um, this other case there, So this saves me from having to have another case, see?

  • And then you know the same code.

  • I can put a comma here, put the other case here, and have either one of these cases use the same block of code.

  • So now you can see that it comes into this case here.

  • The prince is a, B or C instead of coming into default.

  • So why would you want to use a switch statement over an if statement?

  • Well, if there are a lot of different conditions that you want to check on a certain value rather than using an if statement with ah Holton of else ifs, you can use this switch statement, which is easier to read and easier to understand.

  • You're probably going to use the if statement more than the switch statement, but it's still a handy tool to have in your arsenal.

  • So I highly recommend that you try this out.

  • Ah, in your playground.

  • So that's it for switch statements.

  • This is a short one.

  • I hope you enjoyed it.

  • Please help this channel grow by subscribing and hitting that thumbs up button below the video.

  • Hello, Welcome to learn Swift for beginner's Lesson five.

  • In today's lesson, we're going to talk about loops When you're writing your app, there's going to be many times you're gonna find yourself needing to repeat pieces of code.

  • For instance, let's say you're showing five things in your app, and you need to turn each of them read.

  • So Loop is going to allow you to just write that single line of code to turn something red, and you can repeat it five times for each of those five objects.

  • And that's going to save you from having to write out that code five times.

  • So that's a very simplified example.

  • But trust me, you're going to use them a lot, so let's take a look at how to use them.

  • So there are three types of loops we're going to cover today.

  • We're gonna start with the four in Loop, as you can see in the Swift Programming guide here, so let's go ahead and start a new playground in our ex code, and I'm just going to call this the Loop playground and save it on my desktop.

  • Now four in Loop allows you to repeat a piece of code a certain specified number of times If you have a list of data otherwise known as an array, which you're going to learn about in an upcoming lesson, you can also use a four in Luke to go through each of those pieces of data in that array and execute some sort of code on it.

  • So this is perfect for the example we mentioned in the intro about having five objects or five items on the screen, and you need to turn each of them red, for example.

  • So let's start with learning how to use a foreign loop to repeat a piece of code for a specified number of times.

  • And when you guys learn about a raise, I'll show you how to use a foreign loop to go through each piece of data in that array.

  • So as you can see true to its name, the key words to use here are four and in.

  • So you start with the keyword four and next you have a variable name.

  • Now you don't have to declare this variable using the var cured Like you've learned in the previous lessons, you can simply have a name for this variable because you're only going to refer to this variable within the foreign loop.

  • This variable that you specify here is going to keep track of which iteration of the loop is currently running.

  • Next up, you have the key word in, and then you have the lower range, followed by a dot, dot don and then the upper range in that last part There after the in key word is the range which your four loop is going to run in, and it's inclusive of those numbers.

  • So, for example, if you have one dot, dot 1.0.5, it's actually going to run five times.

  • And then after that you open a set of curly brackets and inside the set of curly brackets, that's where you're gonna put the code that you want to repeat for that number of times.

  • So let's jump back to the playground and let's do a quick demo on four in loop.

  • So I have the four key word here, and for my variable, I can specify something like Index and then I would put in and let's use that same example 1 to 5.

  • And then I opened up a set of curly brackets and then here I'm simply going to print Hello, and we're gonna see this in the console down here it is printed it five times.

  • Now, part of the reason why you specify a variable name here called Index is because you want to use that that number or this variable inside your four loop here.

  • So, for example, I can do something like this where I can say print index, and you're gonna see that index changes from 12345 depending on which iteration of the loop it is.

  • It keeps track of basically where we're at in this range right here as we're looping through it.

  • But again, what we've specified here is a counter is only available inside this scope.

  • Here, in between these curly brackets, I can't specify, you know, printing index out here, it's not going to recognize it.

  • Okay, so we can't do that.

  • Um, and furthermore, if you don't need to use index inside your four loop, you can very simply place an underscore there.

  • So this is perfect for the example that we had initially where were just printing hello.

  • Five times We don't really need, um, a counter of any sort.

  • We don't need to refer to index.

  • We don't care which iteration it's currently at.

  • One thing I want to mention, which is confusing often for beginners is this idea of scope here.

  • For example, if I wanted to sum up the numbers from 1 to 5 and I wanted to print out the result after the four loop, let's say I declare a variable appear and I call it some night equated to zero.

  • And then I say something like some plus equals.

  • And actually, let's add this index variable back here and I go like this.

  • Now, you haven't seen this plus equals signed yet, but this operator basically equates to something like this.

  • It takes some and it adds the index.

  • So this is equivalent to writing.

  • This is just kind of like a short form.

  • Okay, so first of all, if I declare this variable called some inside my four loop inside these curly brackets, while this variable is only available within that scope within these two curly brackets.

  • I can't go out here outside of the four loop after its run five times and print out the result of some.

  • See, you can see that it can't find this variable even though I've declared it in here.

  • Okay, so why don't we move this print statement into the four loop?

  • What would you expect to happen in this case?

  • Well, we get 12345 again.

  • And why is it that this sum isn't increasing?

  • Why isn't it continually adding index to it?

  • And you know, why aren't I getting this some of the numbers from 1 to 5 by the last iteration of the loop.

  • Well, what's happening is that, um in the first iteration of the loop, we're declaring some equals zero, and then we're adding index to some, which is one and then we're printing some.

  • So that's why we get one down here in the second it aeration of the loop.

  • What we're doing is we're declaring some again.

  • We're sitting at zero, and we are adding, um, this time the indexes to and we're adding 2 to 0, right?

  • And then it's gonna print too So that's why you can see the output to right there for right now.

  • You might point out to me.

  • Hey, Chris, I thought you said that.

  • You can't read it, Claire.

  • The same variable.

  • You know, in the first iteration, the lute we're declaring var some right.

  • And then in the second generation of the lute, we're re declaring for some that's that's illegal.

  • That's not allowed.

  • In fact, you might say that.

  • You know, if I declare var string again up here, it's gonna throw in error.

  • All right?

  • We can't re declare the same variable because, um, we've declared this guy up here.

  • We can't re declare using the bar again.

  • And I would say that's true, except that in each iteration in the loop, it's almost as if it doesn't remember what happened in the previous iteration.

  • There's no memory or recollection of the previous generation, so each iteration is kind of like a clean slate, and it's gonna execute this code right here.

  • So just to say that again, you're right.

  • If you're saying that I can't read eclair some, all right, because if I tried to do that right here, exclude would throw in error.

  • However, inside of a loop in this scope right here, each iteration of the loop is like a clean slate.

  • Okay, now, if I wanted to actually find out the some of the numbers from 1 to 5, what I would actually do is I would move this declaration up here outside of the loop, and then what I would do This way, I could move this print statement outside of the loop.

  • And what this foreign loop will do is simply loop from 1 to 5 each time adding index to the sum.

  • And now I'm actually keeping track of the sum.

  • So you can see that after five it orations of the loop adding the numbers from 1 to 5 into some, which started out zero.

  • The result is 15 down here, and that's from this print statement.

  • Okay, so I'm going to stop the lesson there.

  • While the syntax for the foreign loop is actually pretty simple, and the concept of repeating a piece of code for a certain number of time is pretty simple.

  • I want to give you some breathing room to digest what we talked about in regards to the variable scope.

  • So I would recommend that you try declaring and creating this loop on your own computer.

  • Try declaring the variables inside the loop and outside the loop and see where you can access them and use them and where you cannot.

  • It's really going to help aid you and you're learning.

  • Hello, Welcome to learn Swift for beginner's Lesson six.

  • In this video, I'd like to introduce you to two more types of loops, and that is the wild loop and the repeat, while loop in these two loops lets you execute some code until some condition is meant.

  • So that's different from the foreign Luke, where it repeated a certain number of times.

  • Okay, let's do it for this lesson.

  • We're going to create a brand new playground, and I'm just going to call this the loop to play around.

  • Okay, so let's cover the wild blue first, take a look at this slide end.

  • Let's go through how to declare a while loop.

  • So the key word here is while followed by that you have some sort of condition, and it's very similar to an if statement.

  • So the wild loop is going to repeat that chunk of code as long as the condition is true.

  • But as soon as that condition becomes false, it's not going to repeat the code anymore.

  • After your condition, you have a set of curly brackets and inside of the curly brackets, that's where you have the code that you want to repeat.

  • So let's do a quick example back in our playground.

  • So I'm just going to declare a variable here.

  • I'm going to call this say Counter and I am going to from said this counter to 10 and then I would like to print hello accounts used five instead.

  • So it doesn't print that many times.

  • And I would like to print hello.

  • As long as the counter is, um above zero.

  • Let's say so.

  • I would say something like while counter is greater than zero.

  • I would I would do this print right here.

  • However, this is going to keep repeating, as you can see, because counter is always five.

  • So inside my wild loop, I'm going to computer is dying.

  • Okay, you can see it eventually overflows and crashes.

  • Okay, so what I'm doing here is I am Decker menting counter by one for each iteration of the loop.

  • So, as you saw when I didn't have this line of code, it just kept printing hello.

  • Many, many times.

  • Because this condition it would always be, um would always be false.

  • There was nothing changing the counter variable from 5 to 0. 00:

Hello.

字幕與單字

單字即點即查 點擊單字可以查詢單字解釋

A2 初級

Swift教程--初學者全科課程 (Swift Tutorial - Full Course for Beginners)

  • 2 0
    林宜悉 發佈於 2021 年 01 月 14 日
影片單字