字幕列表 影片播放
-
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.