Placeholder Image

字幕列表 影片播放

  • TOMMY MACWILLIAM: So let's get started writing our first iOS app.

  • So all of the iOS apps that we write are going

  • to be written in a program called Xcode.

  • So Xcode is an IDE or Integrated Development Environment

  • that's put out by Apple, and it contains all of the tools

  • that you're going to need to write apps for iOS.

  • But before we get into that, let's first take a look at the programming language

  • that you'll be using to write all of your iOS apps.

  • So all of iOS apps are written in Swift.

  • You might see online that there is another language out

  • there called Objective-C that you can also use, but it's a bit outdated.

  • And Swift is what Apple currently recommends

  • to write all of your iOS apps in.

  • So Swift is pretty similar to C, but there

  • are a few differences that we're going to go through right now.

  • The first is variables.

  • So when you're writing in C and you create a variable,

  • every variable is what's called mutable.

  • And a mutable variable means once you have a variable,

  • you can change its value.

  • You can create an integer called x.

  • You can set it equal to 5.

  • And if you want to change it later to be 50, you can do that,

  • and that's no problem.

  • In Swift, there's a distinction between mutable and immutable variables, where

  • an immutable variable once you declare it,

  • you can never change that value again.

  • So think about it as a constant.

  • And, actually, a lot of the variables that you're

  • going to create while you're writing iOS are going to be constant.

  • So if you're creating a constant variable

  • in Swift, which we're going to do a lot of,

  • you are going to start the variable with the word let.

  • If instead you want to make it mutable, you

  • can start the variable with the word var.

  • So if you're ever looking at some source code

  • and you want to know if a variable is mutable or not,

  • you just have to find where it's declared.

  • If it says let, it's not.

  • If it's says var, it is.

  • So Swift just like C has a bunch of built-in data types,

  • and they're pretty similar to what you've seen before.

  • You've got Integers, Doubles, Floats, Strings,

  • and you notice that all of these are capitalized.

  • And Swift also has a few other built-in data

  • types that we'll take a look at like Arrays, Dictionaries, and so on.

  • So here's our first line of Swift that we can write.

  • So you'll see here that we have a variable called title.

  • Because it starts with let, this is an immutable variable,

  • which means we can't change its value.

  • If we try, then the Swift compiler is going to yell at us.

  • And then after the variable's name, we have a colon.

  • And then we have the type of the variable.

  • So it's a little bit different than C.

  • In C, we put the type first and then have the variable name.

  • Here we have the name of the variable first, a colon, and then

  • the type of the variable.

  • So this is a String, and then we have the equals sign and then

  • the value of the variable.

  • So in the next line, we're declaring a mutable variable.

  • So we started it with the word var.

  • We call this variable count.

  • And you'll notice here that I didn't give this variable a type.

  • And this is one of the first new features of Swift

  • that we'll see compared to C is that Swift can

  • infer the type of the variable for you.

  • So because it says count equals 50 and that right hand side of the assignment

  • is an integer, the Swift compiler can look at that

  • and say, OK, count must be an integer because it's

  • being assigned to an integer value.

  • So you don't need to tell me that it's an int.

  • I can figure that out for you.

  • And then in the next line just as we did in C,

  • we can just change the value of that variable and keep going.

  • So, next, let's look at conditions.

  • They're pretty similar to what we've seen before.

  • So in this first line, we're just creating a new constant.

  • It's a string variable of the value of iOS.

  • And then here we have a condition.

  • You'll notice here that in Swift we don't need

  • to put parentheses around conditions.

  • This is kind of nice.

  • Swift basically says, well, we know the thing after the word

  • if is going to be a condition so there's no need to put parentheses around that.

  • So you're not going to see them in any of the code that we write.

  • You can express or and and in the same way

  • you did in C with a double ampersand or a double pipe.

  • And then in Swift to print something to the screen,

  • you can just use the print function.

  • It's similar to printf, but you can print out a whole bunch of different

  • values without needing to do %d, or %f, or any of that stuff.

  • And then else's work the same way.

  • Next, let's look at arrays.

  • So same syntax as before for declaring a variable.

  • We can say let.

  • Then we have the name of our variable, which

  • is values, a colon, and then the type.

  • And you can see here to create an array of integers,

  • I'm going to use brackets and just put Int inside of those brackets.

  • And that tells the Swift compiler that this is an array of integers.

  • And then to declare the array, just open bracket,

  • have your values, and then a close bracket.

  • Again, if we wanted to omit the type of this variable, that would be OK

  • because a Swift compiler can see the right-hand side of this variable

  • is a list of Ints.

  • So that's the type of that's variable.

  • And then to iterate through this array, Swift

  • has this cool operator called for-in.

  • So we can just say for value in values and iterate through this array.

  • So on the first run of this loop, the value of value will be 1,

  • and we'll print out one.

  • On the second iteration of this loop, it'll be 2 and so on.

  • So it's just kind of nice.

  • You don't have to do the whole crazy syntax of Int i equals 0.

  • i is less than values.count. i++.

  • Swift has this kind of syntactic sugar to shorten that a bit for you.

  • So another data type that we'll see a lot in Swift is dictionaries.

  • So you can think of a dictionary as a mapping between keys and values.

  • So in this example here, I've created a dictionary

  • that maps string keys to string values.

  • So you see here in my type that I have in brackets String colon String,

  • and that says that the type of the keys in this dictionary

  • is going to be strings.

  • And then the type of values in this dictionary is also going to be strings.

  • And then when I declare these values, you

  • can see that I have my key, so SFO is our key.

  • And then our value for that key is San Francisco.

  • So what I'm basically doing is mapping airport codes

  • to the names of those airports.

  • So Boston's code is BOS.

  • So I have a key of BOS and a value of Boston.

  • Now to iterate through this dictionary rather,

  • I can use that sort of same syntax as before.

  • I can say for this time in parentheses code, name.

  • Here code is going to be the key in the dictionary,

  • and name is going to be the value in the dictionary.

  • Then I just say the name of the dictionary itself,

  • so I'll say airports.

  • And then I'm going to print out the code and the name.

  • And you'll see here that we have sort of a new syntax

  • here where you'll see this backslash and then in parentheses code

  • and then backslash in parentheses name.

  • This is something that's called string interpolation.

  • And what that means is rather than printing out backslash parentheses

  • code, Swift is actually going to take the value of the variable called code

  • and print that instead.

  • So if you're used to writing C with printf, %d,

  • it's sort of the same thing, just a nicer syntax.

  • Rather than saying %s, code, we can just put in this backslash in parentheses,

  • and then it'll print it out for us.

  • So this is just going to iterate through this entire dictionary.

  • So it'll first print SFO, San Francisco.

  • Then it will print BOS, Boston.

  • So now let's write some functions in Swift.

  • And just like before, you'll notice that there's this sort of type syntax

  • that occurs after the thing that you're creating.

  • So to create a new function, we're going to start

  • with the keyword func, which is kind of an interesting choice for a keyword.

  • But they're all going to start with func.

  • Then you have the name of your function just like before.

  • You notice here that I'm using camelCase rather than using underscores

  • to separate words.

  • Every time I start a new word, I'm going to use a capital letter.

  • And this is just a convention that Swift has,

  • and every programming language has its own convention.

  • But this is what Swift chose.

  • Then inside of the parentheses are my parameters to the function.

  • So this function has one parameter.

  • It's called name.

  • And just like before, we're going to put the type of this parameter

  • after its name.

  • So name is a string, and we're going to use a colon to separate those.

  • And, lastly, we're going to say that my function returns a value,

  • and that value is a string.

  • So we have this little right arrow operator.

  • It's a hyphen and then a greater than, and then we'll

  • have the type that this function returns.

  • So this function returns a string.

  • So the body of this function is pretty simple.

  • We're going to use that string interpolation again

  • by creating a new immutable string variable.

  • It's called message.

  • We can't change its value because we declared it with let.

  • Then we're giving it a value of Hello, and then adding in that name parameter.

  • So with that variable, we're trying to do two simple things.

  • First, we're just going to print it out to the screen,

  • and then we're going to return it.

  • So one other thing you might notice here is that there

  • are no semicolons at the ends of lines.

  • And this is pretty nice.

  • In C, you sort of had to remember every single line ends with a semicolon.

  • If you forgot, your compiler would yell at you.

  • In Swift, you don't need to do that.

  • Semicolons are optional, and so we're just not going to use them anywhere.

  • So the Swift compiler can figure out when your line ends

  • and then move on to the next one.

  • So now to call this function, the syntax is a little bit different.

  • So we're going to have the name of the function sayHello and parentheses

  • again.

  • But then we're going to specify the name of the parameter followed by a value.

  • So you can see here we have name colon Tommy.

  • So we're calling this function with the value of Tommy.

  • But in Swift by default, you're going to include the names of each parameter

  • before their values.

  • And this is kind of nice, especially we have a long function

  • with three or four parameters and kind of hard

  • to remember what the order of those things is.

  • The Swift is much, much easier to read that because each one is

  • going to be prefixed with what the parameter name is,

  • so it's a lot easier to read it.

  • And we'll see a few of those as we write more code later.

  • So like in C, Swift also has this notion of structs.

  • And if you remember, a struct is basically a container for data.

  • It's kind of an object that says, I can have multiple different entries

  • or fields inside of this object, but it represents one thing.

  • So here we have a struct called Course.

  • And here it has two fields.

  • It has a name of the Course, and then it has the instructor for the Course.

  • So the syntax is very similar to what we've seen already.

  • Both of these fields are immutable because they're declared with let,

  • and their data type is String because we have a colon followed by a string.

  • So in addition to structs, Swift also has a more powerful concept

  • called classes.

  • So you can think about a class as basically a struct

  • that can also have functions.

  • So in C when you wrote structs, you basically

  • only gave them field names like integers or strings.

  • But it was hard to put a function inside of that struct.

  • With the class, it's much easier.

  • And when you add a function to a class, we're going to call that a method.

  • So let's take a look at an example.

  • So here we have a class called Person.

  • Rather than starting off our declaration with the word struct,

  • we're going to start it off with the word class.

  • Then the name of our class is person.

  • And it has one field.

  • This field is called name.

  • It's mutable because it starts with the var.

  • And the type of this field is a string.

  • So next is our method.

  • So in Swift, init is a special method that's

  • reserved for when you create an instance of this class.

  • So our constructor takes one parameter.

  • That parameter happens to be called name.

  • It's string type.

  • And then the body of our constructor is pretty simple.

  • All it does is assign this field name to whatever it is the user passed in.

  • And so this last line here is how we instantiate our class.

  • So that's how we create an instance of a person.

  • That instance you'll often see referred to as an object.

  • So, here, same syntax we've seen before.

  • We're saying let, which means this is immutable.

  • The name of our variable is a Person.

  • We don't need a type.

  • The Swift compiler will figure that out for us.

  • And then we're going to invoke the constructor.

  • So we're going to say Person, which is the name of our class,

  • and then we're going to pass in that value as we did before.

  • So here I'm passing in the name of Tommy.

  • And then later I can simply say person.name,

  • and that's going to give me back the value Tommy.

  • So that's how you would create a class.

  • But now let's add a method since we can do that with classes.

  • So, again, we're going to see that same syntax as before.

  • We're going to start off with func.

  • We're going to call our method sayHello.

  • Notice, again, I'm using camelCase to separate words.

  • I'm starting each new with a capital letter.

  • And then the body of that method is pretty simple.

  • We're just going to print out the value of that name.

  • We can sort of assume that that same constructor exists

  • before, so nothing new there.

  • But now if we look at the bottom, after I

  • instantiate that class to call the method,

  • I'm just going to say person.sayHello.

  • You notice that this method didn't take any arguments this time

  • because, instead, we're just using the value that we already

  • passed in in the constructor.

  • And so if I say person.sayHello, this is going

  • to print out to the screen I am Tommy.

  • So another feature of classes that's really powerful is inheritance.

  • With inheritance, you can create a subclass of a given class.

  • So let's suppose that you write out a class.

  • Here's an example here.

  • It's a Vehicle class.

  • Our Vehicle class is pretty simple.

  • It just has two things.

  • It has a function that's called wheels.

  • It returns an Int.

  • And that's just going to tell us how many wheels this vehicle has.

  • And then we have another method.

  • It's called go, and that's just going to print something out to the screen.

  • It goes fast, so it says Zoom!

  • And let's say we then want to create a class that represents a motorcycle.

  • And a motorcycle is really similar to this vehicle class.

  • We probably want to say how many wheels the motorcycle has,

  • and maybe this go method is actually exactly the same.

  • We don't want to change that at all.

  • So what we can do is we can create a subclass of this Vehicle class

  • and override just one of these methods.

  • So here's what that looks like.

  • So here we have a class Motorcycle.

  • And it's inheriting from this Vehicle class.

  • So that's what this colon is.

  • It's saying that Motorcycle is a subclass of Vehicle.

  • And as soon as I do that, every functionality

  • that was in our Vehicle class is now in our Motorcycle class.

  • So if I didn't have anything inside of this class,

  • I could say motorcycle.wheels.

  • That would give me a value of 4.

  • I can say motorcycle.go, and that would print something out to the screen.

  • But, obviously, motorcycles have a different number of wheels

  • than 4 or most of them do at least.

  • And so what I want to do is just override that one method and say,

  • rather than using that definition that I specified

  • before, I want to change that and use this new definition instead.

  • So you'll notice here that my method now starts with the word override.

  • And that tells the Swift compiler this method already existed

  • on that superclass or that parent class, and I know that.

  • And I want to change what this method does.

  • So then I'm going to declare the method in the same way, so func.

  • Make sure it's called wheels.

  • Make sure it also returns an Int.

  • And now when I specify a body, this is the body

  • that will be used if I recall motorcycle.wheels.

  • So we'll have a value of 2.

  • And I can say motorcycle.go.

  • It's going to print out the same thing to the screen.

  • So a few other concepts to cover before we start writing some Swift code,

  • and one is this notion of protocols.

  • When you're writing iOS apps, you're going to see not only a lot of classes

  • but a lot of protocols.

  • If you've ever used other object-oriented languages before,

  • these are called interfaces in most languages,

  • but Swift calls them protocols.

  • And you can think about them basically as a list of methods

  • that a class must implement.

  • So here we have a protocol called Teacher.

  • It starts with the word protocol and then it has a name.

  • And now you'll notice that in this teach function,

  • we actually haven't specified a body, which is really interesting.

  • We're basically saying that in order to be a teacher,

  • you need to know how to teach, but we're not

  • going to tell you how to teach as every teacher might

  • teach in sort of their own way.

  • But in order to be considered a teacher, you

  • must implement or specify a definition for some teach method.

  • So to use this protocol, it's kind of the same syntax as before

  • with inheritance where we can say here's a new class.

  • It's a CS50.

  • And a CS50 teacher is a type of teacher, which means

  • it has to implement this teach method.

  • And so here we are at implements that teach method.

  • We're specifying that name of that method.

  • And we're not going to go into the body of this method

  • because it's probably pretty complicated.

  • But, here, in order for this to compile, we have to specify teach.

  • If we just said class CS50Teacher colon Teacher

  • and tried to compile that, it wouldn't work because the Swift compiler would

  • tell us, hey, in order to implement the teacher protocol,

  • you need to specify a definition for this method.

  • We'll see a lot of this later.

  • One last concept cover is optionals.

  • And this is another thing you'll see a lot when you're writing Swift apps.

  • When you were using C, remember that you could

  • assign null to a lot of variables.

  • You could say I'm not sure what this variable is going to be yet,

  • so let's start it off with a value of null.

  • And then if you remember, if you then tried

  • to use that variable while it was still null, you probably had a bad time.

  • There is some sort of segmentation fault.

  • Something happened that you didn't want to.

  • So Swift really wants to prevent mistakes like that.

  • And so in Swift, you can't just assign null to any variable.

  • Instead, null is basically a special type of a variable called an optional.

  • So in this first line, I'm creating a new variable called name.

  • We're using var because we're going to need to change this later.

  • And the type of this variable is a String,

  • and then you'll see a question mark.

  • So this says the type is an optional string.

  • So the type of this variable could be String,

  • or it could be nil, which is Swift's way of saying null.

  • But in order to use this variable, I need to explicitly check if it's nil.

  • So the way to do that in Swift is with this if let construct.

  • So it looks a little weird the first time you see it.

  • But what this if let n equals name is saying if name is not nil,

  • then assign that value to a new variable called n.

  • And we know for sure that n is not nil at this point

  • when we're inside of this body.

  • So we're just going to print it out.

  • And you'll notice that we're going to print out n instead of printing

  • out name because n is our string variable, and we know it's a string.

  • And then inside of this else, this is going to trigger if name is nil.

  • So if I were to just write this code and then run it,

  • I'm going to be inside of this else block

  • because I've never assigned a value to name.

  • And then it's going to print out that there's no name.

  • So as you're writing Swift for the first time,

  • this can feel really annoying because you're really

  • used to just being able to do whatever you want.

  • And say, oh, I know this isn't null.

  • Don't worry about it.

  • But the Swift compiler really wants to help you make those mistakes.

  • And so tracking down those crazy seg faults

  • is not really a thing in Swift because of the compiler

  • is what's going to prevent you from doing that.

  • You woke up feeling dangerous, and you want to just say, I have this variable.

  • It's technically an optional.

  • Trust me, I know what it is.

  • Then you can just add an exclamation point to the end of it.

  • We'll actually see this a few times as we're writing iOS code just

  • to sort of shorten it.

  • This is dangerous if this name were actually nil.

  • At this point, your app would crash.

  • And Xcode would sort of show you where that line was when you crashed,

  • so still a little better than a seg fault.

  • But we'll see this a few times running iOS apps.

  • But try to really minimize how much you're doing this

  • because this is how you get app crashes is having unexpected unwrapping

  • with this exclamation point.

  • Let's look at one last way to check for optionals.

  • So this is called a guard clause.

  • So remember before when we said if let n equals name, we specified two branches.

  • We said if it is nil, then do this.

  • If it's not nil, do this other thing.

  • A lot of the time when we're writing Swift code, all we really care about

  • is handling the case if it is nil.

  • And if that's true, we probably want to break out of the function we're in

  • or display some sort of error message or something like that.

  • And that's where the guard clause can just kind of shorten the amount of code

  • you have to write.

  • So this is pretty similar syntax as before.

  • Instead of saying if let, we're going to say guard let.

  • Same as before.

  • We're saying n equals name, which means that if name is not nil,

  • we're going to assign it to this new variable called n with the same type.

  • And then we just have one branch here.

  • So we're just shortcutting one.

  • We're saying else do this other thing.

  • In this case, we're just going to print out there's no name

  • and break out of the function we're in.

  • And then after this guard clause, we have

  • this new variable n that we know is of type string, and we can keep using it.

  • So you'll see a lot of guard clauses as I'm writing Swift.

  • I kind of prefer it because let's say that you have got a bunch of optional

  • and you have a bunch of if lets and then you

  • know you're sort of indented six levels deep and it can be kind of crazy,

  • with guards, you can basically say at each point, let's just make sure

  • that everything is sort of what we expect it to be.

  • And if it's not, break out of the function.

  • But if it is, just sort of keep going linearly down the function

  • rather than having a bunch of these different branches.

  • So this is sort of a nice syntax to handle optionals and my preferred one.

  • OK, so let's open up Xcode and write our first few lines of Swift together.

  • So I'm just going to open up Xcode.

  • And when I do, I'll see this little splash screen.

  • And what I want to do is click on Create a New Xcode Project.

  • So if I click that, then I'll have this nice screen.

  • In here, you can see Xcode actually has a bunch of different templates

  • that's built in for me to sort of get me off the ground running with an app.

  • So we'll take a look at some of these later.

  • But for now, we're going to come over here to macOS

  • and click on Command Line Tool.

  • And what this is going to do is basically

  • give us the equivalent of those C programs

  • we had with a little main method that we can just click on Run.

  • Since we're not going to worry about some of the iOS stuff yet,

  • let's just focus on Swift for now.

  • So we're going to do that.

  • Let's click Next.

  • And so here is now some options for your project.

  • So that's going to ask for a product's name.

  • So we'll just call this a Swift example.

  • And then it's going to ask you for an organization name and an organization

  • identifier.

  • So if you're publishing an app to the App Store,

  • this is basically where you'd specify the company you're writing the app for

  • and this organization identifiers, this unique thing

  • that you'll get when you register an App Store account.

  • Because we're not worrying about the App Store right now,

  • don't even worry about that.

  • So I just put CS50 for my organization name.

  • And then a common convention with organization identifiers

  • is it's your domain name backwards.

  • A lot of programming languages do this, and Swift is among them.

  • So CS50's domain is cs50.harvard.edu, so I've done edu.harvard.cs50.

  • And that's my identifier.

  • For language, we're going to make sure that Swift is selected.

  • You notice you can actually write iOS apps in C if you really want to.

  • I wouldn't.

  • We're going to use Swift.

  • Then we're going to come over and click Next.

  • And then it's just going to open up a file finder so you can pick

  • where you want to save your project.

  • You notice here there's this Create Git Repository.

  • If you want to use Git and you're familiar with that,

  • you can leave this checked.

  • If you don't, you can leave it unchecked.

  • Doesn't really matter either way.

  • So I'm just going to leave it checked.

  • And I'm just going to put this on my desktop.

  • And so I will click Create.

  • And so here we are.

  • So this is what Xcode looks like.

  • So in this main area in the middle, this is where

  • we're going to be writing our code.

  • On the left-hand side here is where all of our different files in the project

  • are going to be.

  • And in the right-hand side, you'll see we'll

  • use this to change some properties of different objects

  • we'll create in the project.

  • So this project is really simple.

  • It just has one Swift file called main.swift.

  • If I click on that once, it's going to open it up,

  • and you'll see here that Swift generated a bunch of code for me.

  • It has this sort of nice header comment at the top

  • with the name of the file, the name of the project, my name,

  • it's copyrighted if you're concerned about that,

  • and then some really simple Swift.

  • So the Swift program just starts off this one line import Foundation.

  • This basically just pulls in a bunch of libraries if we want to use them,

  • and we will later, and then just a simple print function.

  • So we know what that's doing.

  • So let's just run this project that Xcode generated for us.

  • So up in the top left here is this little Play button.

  • So if you just click the Play button, what this is going to do

  • is compile this Swift code into an executable.

  • And then it's going to run it for us.

  • So when we're writing iOS apps, this is going to run in a little iOS simulator.

  • Right now, we're just writing sort of plain Swift, so it's not.

  • And down here, Xcode just opened up this little thing for me.

  • I can drag to make it a bit bigger.

  • And this is the output of this terminal app.

  • So as you'd expect, it printed out Hello, World!

  • And then the program ended.

  • This exit code 0-- if you remember back from C,

  • you would often return 0 from your Int main function if nothing went wrong.

  • Swift is basically saying the same thing.

  • Nothing went wrong.

  • This just ran, and that was the output.

  • So we can clear out this print line since we won't need that.

  • And let's write some code.

  • So the first program we're going to write is a little sorting hat program.

  • So what this is going to do is it's going to sort students

  • into these different CS50 tracks.

  • So this is the mobile track.

  • We also have a web track and a games track.

  • And so we're just going to write a little program that will randomly

  • assign students to tracks.

  • So the first thing we want to do is create

  • a new class that represents a track.

  • So I'm going to create this class.

  • I'm going to call it Track.

  • And each Track is going to have a few different fields.

  • It's going to have a name of the Track, so I'll say let name.

  • That's a string.

  • And you notice that Xcode has this really handy autocomplete.

  • Basically, as I continue to type, you'll see this.

  • And so if you just hit Enter, you can take the first suggestion.

  • It can appear as you're typing really fast if you just hit Enter a lot.

  • So each track has a name, and then it also has an instructor.

  • And let's just sort of make that a String too.

  • Again, we'll use autocomplete.

  • And then here you'll notice that this Swift compiler is already

  • being helpful and saying that we have a class,

  • but we haven't created any constructors for that class.

  • And because I have a few fields here that don't have a value,

  • I'll need to create those constructors.

  • So we're just going to create one constructor.

  • So we'll say init.

  • Again, Xcode sort of autocompleting for us.

  • And we have two parameters.

  • So we have one called name.

  • It's a String.

  • We have one called instructor.

  • It's a String.

  • All we want this constructor to do is assign those.

  • So we'll say self.name equals name.

  • self.instructor equals instructor.

  • And there we go.

  • We have a class.

  • We have a class representing a Track.

  • So now let's create our list of Tracks.

  • So we have these three different Tracks, Mobile, Web, and Gaming.

  • So let's create a new list called tracks.

  • If we want, we can give this a type of Track.

  • We don't need to since the Swift compiler is

  • going to figure that out for us.

  • And so we'll open up our new array.

  • And so now we're going to instantiate three Tracks.

  • So remember to do that, we're just going to say the name of our Track.

  • Once I hit open parentheses, Xcode is going to be really helpful

  • and say here's the parameters to that one constructor you created.

  • So I just hit Enter.

  • And it's going to fill all this in for me.

  • So let's start.

  • Let's say the name of our Track is Mobile.

  • And then if I want to jump over to this next place holder

  • here after instructor, if I just hit Tab, my cursor is going to jump over.

  • So I can say the first instructor is Tommy.

  • And then the next Track, let's say is Web.

  • That instructor is Brian.

  • And then the last Track is Games, and that instructor is Colton.

  • And so there we go.

  • So now I have created an array of Tracks that has three values, and each of them

  • represents a different instance of this class.

  • So if I want, I can just sort of make sure that this is compiled.

  • If I hit Command-B, that's going to build the project

  • or compile it but not run it.

  • So I'm just going to hit Command-B. You'll see Build Succeeded.

  • That means that there were no compilation errors.

  • Now, if I for example forgot a comma here and then I hit Command-B,

  • you'll notice that I have this Build Failed.

  • And this Xcode is going to highlight in red where my syntax error is.

  • So it's really neat is if I click this little red circle here,

  • Xcode is going to offer to fix my code for me.

  • So if I just click Fix, it's going to insert that comma for me,

  • and then I can move on with my day.

  • And just to sanity check, if we take out the type of this Track's variable,

  • if I remove that, then click Build, we're

  • still going to succeed because Swift is going

  • to say at right-hand side of this expression is a list of three Tracks.

  • So, obviously, the type of this has to be a list of Tracks.

  • OK, so we've got that.

  • So now let's define a list of students.

  • So let's say this time we have some list of students.

  • We know we don't have to give this a type.

  • So let's just call these three students three totally random names

  • that are really common like Harry, Ron, and Hermione.

  • That's our list of students, and we're also

  • going to want to have some variable to keep track of how we're going

  • to assign these students to Tracks.

  • And so a really nice way to do this would be with a dictionary

  • where the keys in our dictionary can be students

  • and the values of those dictionaries can be the track that they're assigned to.

  • So we know that we're going to need to change and update this dictionary.

  • So what that means, instead of using let as we've

  • been doing for most of the time here, we're now going to use var.

  • So let's say my var assignments.

  • And this time we do need to specify a type because initially this dictionary

  • is going to be empty.

  • So because it's empty, Swift has no idea what kind of type it is.

  • So we're going to say that this dictionary is

  • a mapping from String to Track.

  • So the keys are Strings.

  • The values are Tracks.

  • But, initially, it's just going to be empty.

  • So to create an empty dictionary in Swift, that's our syntax,

  • open bracket, colon, closed bracket.

  • If this was just an empty array, I could do this, but it's a dictionary.

  • So we need that colon.

  • OK, so let's think about the logic here.

  • So what we want to do is iterate through each element in our students array,

  • decide on a random Track, and then assign that Track to the student's name

  • in this assignments list.

  • So to start, let's just loop over our students.

  • So we can say for student in students.

  • Again, Xcode helped you out there with the autocomplete.

  • I didn't have to specify any types at all.

  • We just sort of understand that student is now

  • a String because every element in this students list is a string.

  • So just a sanity check and make sure on the right track,

  • let's just print out each student's name.

  • So we can say print student.

  • And then we're going to run it.

  • So if you can just hit Command-R to run.

  • Compiled OK.

  • And then, yeah, this is what we expected.

  • So here we have the students in the order we declared them all printed out.

  • So that's not super interesting.

  • So let's figure out how to assign them to a random Track.

  • So we want to say, let's create a new variable called Track.

  • And we want to set that equal to a random Track.

  • We're just going to have a really simple sort here.

  • And so what we want to do is generate a random number between 0

  • and the number of tracks that we have.

  • And this is where Xcode autocomplete can be really helpful.

  • You can just sort of start typing and then browse the autocomplete

  • to see what they suggest.

  • So I happen to know that to generate a random number I'm going to say Int dot,

  • and then I have this random method.

  • But if I was also curious what other methods are on this Int,

  • I can just sort of start typing and then just sort of browse.

  • And it's going to tell me all about what these methods do

  • and what their parameters are, but I wanted this random one.

  • So you can see here that Xcode will tell me

  • that this returns a random value, then a specified range.

  • Great, that's what I want.

  • So I'll hit Enter.

  • And now I'm going to specify a range in Swift.

  • So this is kind of cool, but you can say 0 dot dot less than

  • and then the size of the range.

  • So this is going to be tracks.

  • And I'm going to hit Enter.

  • And tracks.count is going to give me the total number of elements in that tracks

  • list.

  • So this syntax is basically a way of saying

  • I want sort of a range of numbers.

  • I want the start to be 0, and I want the end to be the size of the tracks array.

  • So what this is going to do is generate a random number between 0 and 3

  • exclusive or 0 and 2 inclusive.

  • So that's my random number.

  • So now I want to assign that Track to a student.

  • So to do that, I'm going to say assignments.

  • As my key, I'm going to specify student.

  • So this says inside of this dictionary I want

  • to create a new value for the key student,

  • and then I want to give that a value.

  • So then I can just say tracks, and I'm going to index

  • into that array just like I did in C with my new integer track,

  • and that's it.

  • So I notice that those warnings went away.

  • The Swift compiler is happy with me, and now I've

  • iterated through those students and assigned them a Track.

  • So, last, we want to make sure that this works,

  • so let's just print out the assignments.

  • So remember before to iterate through a dictionary,

  • we're going to create a new for-in loop.

  • But this time you need to specify the key and the value,

  • so let's say for student track in assignments.

  • So here now we have two variables.

  • This first variable has a type of string.

  • The second variable has a type of track because that's what our map was.

  • And let's just print out the assignment.

  • So we're going to say print out.

  • And then we're going to start using this variable interpolation.

  • So I'm going to say backslash and parens.

  • Now I can start typing student.

  • And what's really cool is Xcode is going to notice that you're

  • in this backslash parens thing.

  • So if you start typing, it's going to autocomplete with variable names you

  • might want to print out.

  • So if I just say student, you'll see it sort of start autocompleting for me.

  • I'll just hit Enter.

  • So there's the name of the student, and then we

  • can say the student got my track.

  • And then I don't want to just print out the object.

  • I want to print out one of those fields.

  • So I can say track.name and then say with track.instructor.

  • So there we go.

  • So here I'm just printing out one line, but I've

  • used this variable interpolation a few different times, the first one

  • to print out the value of student.

  • And then take this object and just convert it into this nice string

  • by saying track.name, track.instructor.

  • If we hit Run, we get Build Succeeded.

  • And there we go.

  • Nobody is taking Mobile, so we're just going to have to click Run again.

  • Still no one's taking Mobile.

  • There we go.

  • I think I got my favorite student here.

  • So each time you're running, you're going

  • to get a different random list of assignments.

  • And so that's it for our introduction to Swift.

  • This covers basically all of the syntax details

  • you're going to need to start writing some simple apps in Swift.

  • And so in the next video, let's start writing our first iOS app.

TOMMY MACWILLIAM: So let's get started writing our first iOS app.

字幕與單字

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

B1 中級 美國腔

CS50軌跡2019-iOS-第1課時 (CS50 Tracks 2019 - iOS - Lesson 1)

  • 10 0
    Prova 發佈於 2021 年 01 月 14 日
影片單字