[MUSIC]
Stanford University. >> Okay, well welcome then,
to Stanford CS193P. This is Developing Applications for
iOS. Hopefully you're in the right place.
This is fall quarter of 2017. And I'm gonna breeze through
a few slides here. And then I'm gonna sit down and
do a nice involved demo just to show you what it's like
developing apps for iOS. That's the best way to learn,
it's for me to show you how to do it. So
this will be what I'm gonna talk about briefly first.
So what are you gonna learn in this course?
Of course you're gonna learn how to build cool apps.
iOS apps are cool. Why are they cool?
Well, they're cool because they live in your pocket, you
can show them to your friends right there, also incredibly
easy to sell and market your apps online because you have
the App Store. And there's a very vibrant community that's
because Apple is always doing cool new things like virtual
reality and machine learning and all these things.
And there are millions of apps on the app store, so there's
a lot of people doing this stuff. But you're also gonna
learn in this class a little bit of real life
Object-Oriented Programming.
You all are taking classes networking and databases and
graphics, your other CS classes. And here we're gonna
do all those things in a real life platform.
Because over the weeks we are going to do all of those
computer science things for real. And especially
Object-Oriented Programming, cuz iOS is completely and
utterly object-oriented. You cannot develop for
iOS without being serious object oriented programming.
And that brings me to my prerequisite for this course,
which is Object-Oriented Programming. You have to be
comfortable with Object-Oriented Programming.
Definitely CS106 A and B and hopefully CS108,
which is Object-Oriented Programming here at
Stanford. And the other thing you gotta to be comfortable
with is writing a lot of code. All your homework in this
class, there's a little bit of reading the first few weeks
about the language that we're gonna develop in.
But mostly it's just programming, programming and
more programming. So if you're not comfortable writing a lot
of code then this class, you might wanna wait until
you've taken some other classes that ask you to
write a lot of code before you dive in here.
So what's in iOS? What am I gonna be teaching you?
I've divided it here into four layers.
The bottom layer is near the hardware, and
the top layer is near the user. So at that bottom layer,
you might be surprised to find out that iOS is Unix.
It's a BSD variant of Unix. And so all of the stuff
that's going on down there is all happening in C basically.
Unix is mostly written in C, most of that stuff is in C.
I'm not gonna teach you anything at this layer.
This is an object-oriented class,
everything we do will be object-oriented.
You will not see me teaching you anything down there,
The next layer up is called the Core Services layer.
all right?
This is an object-oriented layer on top of those lower
services. This I will be teaching you. So if we wanna
do things like find out where the phone is on the planet or
find out its orientation or access some files in the file
system, we're gonna be using this layer right here and I'll
be teaching you all about this layer. Core Services layer.
The next layer up, this is kind of not a strict layering,
but is the Media layer. Don't forget that your iPhone
is essentially, originally an iPod with a phone in it. So
it's got all kinds of media, video, audio,
many different kinds, images, all this stuff. And
unfortunately this is a layer I'd love to teach you but
I have to cut something, so I'm not gonna talk much about
this layer, unfortunately. So I know some of you might be
wanting to write a cool game that has 3D sound with TIE
fighters ripping around from the back of you to the front.
And that's all possible, and quite straightforward to do,
but in ten weeks I have to pick what I'm gonna teach you,
and so I'm gonna focus on Core Services. And then this layer,
which is Cocoa Touch. So Cocoa Touch is the UI layer of iOS.
This is where the buttons and
the sliders, all those things are in. And
there's also, really powerful objects here, like the Map Kit
has a single object that you can just drop in your app
that gives you pretty much the entire functionality of
the map's app inside a rectangle, inside your window.
So, you're talking about a wide variety of UI stuff here
and I'm gonna try and cover as much of this as I can. So
that's what you're gonna learn in this class.
The platform we're gonna develop on is Xcode 9, so
you're all gonna need to download Xcode 9,
it's free and it only runs on your Mac.
And, there's this other little app, Instruments, but it's
really kind of just an add-on to Xcode 9. We're gonna do
everything in Xcode 9, source code, editing, debugging,
all that stuff is gonna happen in Xcode 9. You're also gonna
have to learn a new language. iOS has two languages you can
develop in, Objective-C and Swift.
Swift is the newer one, that's the one I'm gonna teach you.
Everything you learn though in Swift, all about iOS,
will apply if you later wanna go learn Objective-C and
work for a company that is still writing in Objective-C,
which is perfectly valid language to write in.
But Swift kind of a cool new language,
I think you're really, really gonna like it.
If you're a serious Computer Scientist another language is
like big yawn whatever. Just tell me what the syntax is,
tell me what they key fundamental mechanisms for
designing with it are, and I'll learn it.
So, if you don't have that attitude towards languages,
you're gonna be kind of having trouble out in the real world
being a programmer. And then of course,
there's millions of what we call Frameworks in iOS.
Frameworks are just collections of objects.
Like the biggest one is UIKit, that's where buttons, and
sliders, and all that stuff is. Foundation is another
big one. That's where a lot of that core services stuff I was
talking about is. But there's the Map Kit, Core Motion,
Core Data, Object Orient, Database, all kinds of them.
And I'll try and get to as many of them as I can. And
last but definitely not least, there is a Design Strategy for
building iOS apps that you have to use.
This is not like an optional, here's a good idea, why don't
you design this way? You must design this way. It's called
MVC, Model View Controller. How many people have,
some experience doing MVC in any other? Okay, it's not so
many this quarter. Usually I get half the people but
I'll be teaching all about MVC.
The start of electron Wednesday is gonna be a full
coverage of MVC. What it is, how it works, all that stuff,
all right? So now, I'm gonna dive right into a big demo
that's the best way to learn how to do iOS development is
with a demo. Seeing it happen, we're gonna build
an application from scratch. This slide right here,
is a slide for you to look at later, and
see, did I learn all these things, because I should have
learned them today. This is not a slide to read now.
Since I'm not gonna get back to the slides,
let me just say what's coming up real quick. You are gonna
have a reading assignment that goes out today.
It's basically starting to read the manual on
Swift. So you can learn this new language. It'll be all
spreaded out over three maybe four weeks, so you don't have
too much reading all at once. >> But
those reading assignments are gonna be in addition to
programming assignments. The reading assignments are going
to go out on Monday, they come back on Monday,
they're due the next Monday. And
then the program assignments go out on Wednesday and
then they're due the next Wednesday generally.
Okay, at least we'll start the quarter that way.
On Friday, we have an optional section,
it means optional that you don't have to go there.
The topics we're gonna cover are kind of additional, but
this Friday is one is a big one, it's tips and
tricks of Xcode, including how to use the debugger.
So if you've never used the debugger in Xcode,
this is a good one to go to, so
you can see how to use the debugger for your problem.
If not, you know, it's a debugger, if you used other
debuggers, you could probably figure it out, but anyway, it
says right there that it's in Hewlett 205, but I don't think
there's such a room. I think it might be in Hewlett 105.
I don't know, watch Piazza and we'll tell you where it is.
But it is gonna be 11:30 to 12:20. And so next week we'll
talk more about Swift and then launch into all of iOS,
all right? So let's jump right into the demo here.
And I apologize in advance for going fast on this demo,
because we have a lot to cover, and
you're gonna find that in this course, in general,
I tend to go pretty quick, cuz I wanna teach you as much of
this stuff as I possibly can. All right,
Xcode. Here's Xcode. I went to the Mac App Store.
I searched Xcode, I found it. I downloaded it.
It was free, it doesn't cost me a dime to develop for
iOS. When you launch Xcode for the first time,
you're gonna get this thing right here. So
we need to build an app. Now, I decided to show you the app
that I'm gonna build here in real life. So you see these
cards that I've very artfully taped up here? These cards
are going to let me play a game called Concentration.
How many people have heard of the game Concentration?
Not too many. Okay, so Concentration is just a card
game. Behind these cards that are all face down,
there are some pictures, and the goal is for
you to match the pictures up. So there's 12 cards,
6 pairs of pictures, so I get to pick two cards at a time,
and if they match, I win, and
the cards go away. If they don't match,
I have to turn them back down and pick two other cards, and
it's called Concentration cuz I have to concentrate on
the ones that didn't match, so that later I can go back and
match them. So, let's just flip some cards over here.
I'm gonna start with this one right here.
I really don't know what's behind there, but let's see.
Okay, this one is a pair of purple bats.
So now I'm trying to find another pair of purple bats,
let's try right here. I found them right away, okay.
So this is a match, I get some points.
These come off the board. So now I'm searching for
more matches. All right, let's try this one right here. It's
a ghost. I got the ghost right there. Let's try this one.
No, no match. That looks like a Rorschach thing, but
it's a witch. Turns out that's a witch on her broom,
if you can see right there. So, I don't get any points for
this and I have to turn these back down and pick two other
cards. All right, so I'll turn these down.
Hopefully you guys are concentrating, and
you can remember what these things are. I'm gonna try this
one. It's a cat, kind of a cute cat right there, and
let's pick another one, how about this one. A pumpkin!
Okay, well, in some ways, this is bad,
because we have another non match,
but it's kind of good in that supposedly,
if we we're concentrating we know where four cards are. So
turn these back down. Pick two new ones, and
I'm gonna pick cards that I haven't seen before,
because I already know where these cards are.
All right, that doesn't fall down.
All right, how about this one. This one is, The witch,
the witch! Okay, so now if I was concentrating,
I remember where the witch was, now where was it? Was it,
I think, was it this one? Yeah? Witch, yes, okay!
So we match these, and these come off the board, and this
just continues, and obviously the fewer things you choose,
okay, the fewer times that you pick, like here is a cat.
Where's that cat? I think it's down here.
No, that's not a cat. Now this kind of a mismatch
I should be probably get a big penalty for,
because the cat I've seen before, it's right here.
So I should have matched it, and I didn't,
I wasn't concentrating. So this is the game we're gonna
build an app to do. All right, make sense? Simple game.
All right back to Xcode right here. Now this Xcode portal,
the splash screen that pops up here.
It has all the apps that you've been working on all
quarter. Right here, and then it lets you create a new app
over here where it says create a new Xcode project. So,
we're gonna create a new Xcode project.
I just click that button. When you create a project,
it asks you what kind of project do you wanna build?
And we wanna build an iOS app, you see up here at the top.
We could build a watch app, or an Apple TV app,
but we're building an iOS app, and
this is saying what kind of iOS app would you like?
So here's a game, augmented reality. We're always gonna
pick this one in the upper left corner, single view app,
because it's the simplest starting template, and I wanna
teach you how to write the code to do all those
other ones. I don't want you just click a template, and
it just shows you the infrastructure for a game, or
whatever. I wanna show you how to do that.
So we're always gonna pick single view app.
When we pick the kind of app we want it's gonna ask us some
questions like what do you wanna call this app?
Well this game is called Concentration, so I'm gonna
say Concentration. The next one down here is Team.
That's the development team working on it. That's you. Now
when are on it it's gonna say Add Team or Make Team right
there. To me, it knows it's me, and when you do that,
all you're gonna need to create a development team is
an Apple ID. You don't have to pay any money.
Any Apple ID will do, go through the process.
You'll creat a development team. Next,
is this Organization Name. That can be anything you want.
It just shows up in the copyright in your Swift files,
all right? But this one, very important that this be
uniquely identifying you. So a real easy way to
do that if you're a Stanford student is
edu.stanford.cs193p, your SUNet id.
Instead of lecture there, put your SUNet ID.
That's clearly gonna identify you. If you're not a Stanford
student, you have to figure it out on your own, and
then the language I told you about, the two languages,
we're gonna do all our development in Swift. You can
mix Swift and Objective-C even in the same application.
They're very interoperable. Swift was designed fully with
Objective-C in mind, so it's not a problem. So we'll always
pick Swift here, and we're not gonna do any of these
things down here in the first two weeks,
but we will eventually get it, object-oriented databases, and
testing, we'll eventually do that stuff.
So I'm gonna click next.
Now it's asking me where do you wanna store your project?
I strongly recommend you put it in your home directory,
in a folder called Developer, that's the kind of a canonical
place that people put their projects.
All your projects will collect here, okay, concentration, and
then the other ones we're gonna do later in the quarter.
This source control,
we'll talk about that later in the quarter as well, but
we're not doing that for the first week. Okay here it is,
your first iOS app. Now what we're seeing in the middle of
Xcode, how many people have done something in Xcode
before? Okay, so about half of you, so that's pretty common.
So you know that in Xcode, this middle area is your main
editor, and right, we're seeing right now,
in this main area, is our project settings.
We answered some of these in those previous little things
that came up, and the reason it's showing our project
settings is because on the left here in this blue area
we have the project selected, see the dark blue selection at
the top. That's the project selected, and
this whole blue area on the left is called the navigator,
and the navigator let you navigate through your project,
and in this particular tab it's showing me the files in
my project. I have six files right here,
they were given to me chose that template, the single
view template, but I can also now navigate by searching,
right here. If I'm debugging I can navigate through my break
points, etc. So you'll get real familiar with using this
blue thing right here,
and you can say how much space you want it to have to
navigate through your application.
Now on the right hand side, this over here,
you can actually see a little area that has a top and
a bottom. This is called the utilities pane, and
I'm gonna show you all about that in five minutes.
So I'm gonna hide it right now.
You see this button in the very upper right.
That hides that, and this one hides the navigator. So if you
wanna give more space to your main window, you can do that.
There's another button here, you see that one?
That hides your debugger and console window.
So this is the debugger right here where you're looking, I
think, going on your debugger, and this is the console.
Now, the console is just a place where there's a function
in Swift where we can print text out there.
It's really great for lightweight debugging.
We're just printing out what's going on. Inside my app.
And that's what I'm going to do today,
I'm not going to show you the debugger today, that's for
Friday. I'm just going to show you using print to debug and
you can also move this up and down as high as you want. So,
it's very nice to be able to, kind of, organize your space.
I have a fairly low resolution screen here. So
I'm going to be trying to hide things as much as possible to
make the text really big so you can see what I'm doing.
All right, one last thing I want to show before we dive in
here, is this area up here, you see this, this is how you
run your application. Okay so, when you run your application,
you have to decide where are you gonna run it. You can run
it on a device, so you can hook up a device to your Mac.
And you can hook it up wirelessly actually or
with a little USB cable or whatever. And I don't have any
hooked up, so I can't do that. But I can also can run on all
of these simulators. So, these simulators simulate these
various device like an iPad or iPhone 8+ or whatever. And
you can run your app on any of these that you want and
in fact, the simulator will open both of these,
multiple of these as a time, if you want. So I'm gonna
do the latest and greatest here which is the iPhone X.
Okay I'm gonna run my app on IPhone X and
let's go ahead and run it. We haven't done anything, so
it's just a blank app and let's just run it for fun.
You see this play button right here? That's how you run, so
I'm gonna click that play button. Now it is compiling my
app as you can see at the top, it's it building into
a binary. It's loading it onto the device, or
in this case, simulator. And it runs it. Okay so
here it is. It looks like an iPhone X and
it's running my app and it's completely blank.
Well because I haven't done anything,
I haven't built any UI. But if I press the home button
on the iPhone X, everyone know how that works? You slide up
from the bottom. Sliding up from the bottom is like
the home button because there is no home button in iPhone X.
And you can see there's our app concentration right there.
But all other apps are there too.
Settings, right, you can go into the settings app. And
if you want, you might have your app needs to set some
setting like I dunno text size or something like that.
So these simulators are truly simulating the device, they're
not just running your app only. So that's kinda cool.
And we can always go back to our app. Just go back here and
say Concentration, here's Concentration. All right,
so now we're familiar with Xcode and kind of
all its pieces right here. Let's take a look at the files
that were created for us. Now, There are six files but
actually we're only gonna look at two of them. For example,
this one, assets, right here, that's your images,
including your app icon, which we haven't set,
I don't really need that. Your launch screen right here, I'm
selecting that as well. This app delegate up here, we don't
need to touch any of these to make our Concentration app. So
I'm selecting them all and I'm going to right-click on them,
and go down to new group from selection, and put them in
a group. And I'm going to call that group Supporting Files,
because that's what they are,
they're supporting files. By the way, I would not
put your info.plist in your Supporting Files,
leave that at the top level. It seems to be better. So
this other file here, ViewController.swift,
is just some Swift code, we'll get to that in a second. And
right here, Main.storyboard, that's your UI.
Now you're gonna build your UI in Xcode graphically,
okay you're not gonna code it up, you're gonna do it
graphically. And not only that the way you're gonna build it,
you're gonna drag out buttons and
text fields and sliders and things like that.
And it's going to actually put real buttons and
real sliders live on the screen at a time. And
you're going to edit them and set them up the way you want.
And then when you run them on your app,
it kind of freeze dries them, brings them over and
adds water and they come back to life. And then they run. So
when you run, it's not like you don't click a button here
and it generates a lot of code to put those buttons there.
It's actually editing these buttons live. Now,
notice that I got this, sort of, iPhone shaped rectangle.
Can you all see that? Yeah, it looks pretty good. This iPhone
shaped rectangle is where I'm going to edit my UI. And in
particular this is an iPhone 8 sized rectangle, you see that?
It says view as iPhone 8 down here. I can click on this and
actually look at other iOS devices. So I can look at
my IU, how it would look let's say on an iPad.
Now an iPad is huge so maybe I wanna zoom down a little bit
to see it better. I can also look at it in landscape mode.
So here's portrait and landscape of an iPad, for
example. I could go to a different kind of iPhone,
like here's my iPhone X, all right? So I can look at
all possible iOS devices and see how my UI looks.
Now our goal eventually, once we learn enough about iOS,
is to build UIs that look good even if they're in landscape
or portrait, iPad, iPhone 8, iPhone 8+, which is big,
iPhone X, it doesn't matter. Okay that's our goal.
Now in the first week, I can't teach you any of that, so
we're gonna have our UI just
try and look good on an iPhone X.
But next week, not to worry we'll try to make it so
it looks good on other iPhones as well. And
your assignment two is gonna really ask you to make
an app that just looks good on all possible iOS devices.
Now on the left over here, when we brought up this,
this area in Xcode by the way is called Interface Builder
because we're building our user interface,
so Interface Builder. This area over here is essentially
all the things that are here but
in text form, like a text outline.
And I'm gonna hide that,
you do that with this little button down here,
see it says hide document outline.
I'm gonna click that to hide that. We'll go and hide this
phone chooser as well and we have our little UI here.
We'll look at that document outline later in the quarter.
I want to build my UI, how do I build a UI?
Okay I'm trying to build Concentration here.
So I'm going to need some cards. And I'm going to
use a button to represent a card, which is kind of cool,
because you tap on a button and
it does something. And that's good,
because when you tap on a button I want it to flip over.
Okay, I'm going to have my cards flip over. So
that's the first thing you do. Let's build one card that will
flip over when we click on it all right. So
I'm going to zoom in, by the way, you can hold down alt.
You see in the lower left I have a thing here that shows
when I'm pressing alt, and control, and command,
and all that stuff. If you hold down alt and
use your mouse wheel you can zoom in and out, on your
UI, which is kind of cool. Otherwise your mouse wheel
moves it up and down, which is also kinda cool. All right so
we have our UI here, how do we put a button in this UI? Okay,
as promised, we go over to this thing that had a top and
a bottom, called the utilities pane.
And we're going to look in the bottom to start, and
specifically, in this object library tab. And
we're going to see a whole bunch of objects, remember,
and I mean objects like in an object-oriented sense,
this is all object-oriented. And all these objects,
like labels and buttons and text fields and switches,
even web views and table views and text views and even ARKit.
Here's a little ARKit of augmented reality views.
These things can all be dragged out,
multi touch gestures, dragged out to build your user
interface. Okay and I'm going to cover the vast majority of
the stuff in this very large list right here.
But we're going to start with a simple one which is towards
the top, which is buttons. So
I just wanna drag a button into my UI, and
I just drag it in, I pick it up with mouse, and
drop it in my UI. Now, when I move this around,
look at these blue lines, see these blue lines.
They're trying to help me put it exactly in the middle, or
exactly in the middle on the bottom edge,
or up in the upper right corner.
These blue lines are critical to building a UI that will
work when you rotate or on a bigger device or whatever. But
we're gonna ignore the blue lines for now
because I said I'm gonna teach you that next week,
all right? So here's my button, it's very, very small.
I want it to be bigger. Well, I can just grab these handles
on the edge here and resize it to whatever size that I want.
Maybe make it a little more card shaped or
something like that. And I could also edit the text on
here by double-clicking on this.
I don't want Button maybe, but let's do the back of the card,
let's see what the back of the card looks like.
So I'm just gonna delete it,
so now my button has no text on it, all right,
which is fine. How about setting the background color
to orange, these cards orange, and our Halloween theme here,
they have orange background. How do I do that?
Well, that's the top of this utilities pane. Notice that
when I select a button, I get this UI over here that is
specific to setting the attributes of a button. And
not only that, it's object-oriented. So
I have the button stuff here and as I scroll down, look,
I get UI for a control because button inherits from control.
And then control inherits from view. So
I get UI to enter to view because a button is a view,
right? It inherits from view in the object-oriented sense.
So the background of any view is settable here in this UI.
It's right here under View, it says Background.
Right now the background is clear, that little line
through a thing means clear. So there is no background, so
it's showing the white from behind is showing through. But
I can easily change this to orange by clicking there and
going down here. You know,
there's a lot of preset colors here.
I can go to Other and pick it by name, I could pick orange.
I could pick a crayon that looks orange,
color wheel, whatever. But here I want orange, so
I'll just pick it by name. And
now I've set the background of my card to be orange, so
that's good. That's looking pretty much exactly like this,
I'm happy with it so far. And in our Halloween theme,
to make it even scarier, let's make the background here
be black. So I'm gonna change this to be black.
And the way I do that is I just click on it and
now this right-hand side doesn't say button on the top.
It say view because this big space is just a blank view.
It's not a button view or anything. It's just a view,
but it's got this background thing, same way. So
I click on it, and here I'll just pick it from this list of
predefined things. Black color, and now, I have a nice
orange on black UI. Looking pretty cool. Now, what about
when the cards are face up? Well, when they're face up,
they're white. And then they have an image on them.
Now I could have the cat or the spider web here or
whatever be some JPEG image or something. But
I got a really cool idea to make this be real easy, let's
make it be an emoji. Cuz if we just put an emoji in here,
then we have a lot of choices. And it's really easy and
we don't have to go find the images or anything like that.
So that's what we're gonna do. So
I'm gonna make this look like the front of the card. So I'm
gonna go down here and change this orange back to white,
okay, this white color. And I'm gonna put on here,
instead of blank, I'm gonna put an emoji. Most Mac apps,
if you go to their Edit menu, at the bottom of it,
there's this emoji and symbols. I don't know how, how
many of you know that. But if you go to emoji and symbols,
you get this window right here that lets you choose
all these various emoji. We got a Halloween theme here, so
let's look for a ghost. Yeah, Mr. Ghost, right there.
Okay, so that's a good, he looks like a fun ghost even.
We'll double-click right there to put him on the card and
there he is. He's very, very tiny. I could go zoom in and
look at them. But he's even small compared to the size of
the iPhone X. If my user was doing this, they'd be like,
what is that. It was just like a little smudge. So
we wanna make this much larger. So again,
I'm going to the top side, top half of this utilities pane.
And if I look down here, look, font, system font 15 point.
So I'm gonna make that way bigger,
let's make it 50 point.
Now we got a nice big ghost. Awesome,
we have got all we needed in terms of design to do our UI.
Let's go ahead and run and see what happens here.
See if this black background and this ghost and
all this stuff is showing up in our app when we run.
And of course it will be, here it is. And when I click on it,
can you see that it's flashing a little bit there?
That's saying, that's giving me a little feedback, yeah,
you touched on that.
It's nice, but it doesn't do anything, right?
And we want it to do something.
Mainly we want it to flip over. So let's go do that.
How do we make it so something in the UI does something?
Well, we're gonna hook it up to some code. Specifically,
we're gonna hook it up to this Swift code right here.
Now they gave us these things right here,
which I'm gonna delete so as not to distract you.
But this is your first look at Swift code.
Let's look at it real quick, see what it does. Import,
it's just like include. It's just bringing all of UIKit
in for our use. UIKit is iOS's framework that has buttons and
sliders and all that stuff.
Kinda that top cocoa touch layer we're talking about.
So this is the declaration of a class, in an object-oriented
sense, a class. Of course we use the keyword class.
This is the name of the class, ViewController.
This is not a very good name, it's very generic. Probably
this wants to be called ConcentrationViewController or
something like that. Do not change the name of this in
your homework, okay? Changing the name of
this requires a little more than just changing it
right here, because it's also showing up in your UI.
So you have to change it in away that it changes in both
places. This is its super class. This is
Object-Oriented Programming. We have inheritance.
It's inheriting from UIViewController.
Class UIViewController is in UIKit. You can tell because it
starts with UI. And what that class does,
it knows everything about controlling a UI like this.
That's what it does. So by having our ViewController
inherit from that, it inherits all the capability to control
that thing. So all we have to do is put the code that has
to do with the Concentration game in here, so
that's great. Then inside this curly brace, we're gonna
put all our methods and instance variables. Who
does not know what the word method or instance variable
means? Okay, good, everybody does. And you should because
that's fundamentally an Object-Oriented Programming,
right? So we're gonna put all our instance variables and
methods inside those curly braces.
That's how we're gonna declare our class. So how are we
gonna make this button do something here? Well,
what we're gonna do is we're gonna create a method in here.
And we're gonna make it so
that when you press this button, it calls that method,
of course. How are we gonna do that?
To do that, believe it or not, we need to get this UI and
this code over here on the screen at the same time.
And we do that with this little button right here.
This little circles thing right here,
this is called the assistant editor. And when I click it,
they come on screen, both of them on screen, at the same
time. And I can adjust this space however I want.
Maybe we'll get rid of this now.
All right, so here's my UI and my code on screen at the same
time. Now, why do I want them on screen at the same time if
I need to hook them up? Because the way I hook them
up is I hold down the Ctrl key and drag a line from the UI to
my code. I know that's kinda weird, but
that's how we do it. And when we let go of that drag line,
it says, you will make a connection between UI and
code. No problem, what kinda connection would you like?
You can make an action, which is a method, or
one of these two outlet things,
which I'm gonna talk about later in this lecture. So
let's start with this action one. An Action connection
means when this button is pressed, call a method.
And I get to say what the name of the method is.
I'm gonna call it touchCard, cuz that's what's happening,
someone's touching on this card. And this method that's
gonna be created for me, it can have arguments,
it could have no arguments, that's none. Or
it could have one argument, which is the sender,
in other words, the button that is sending me this.
I really need that argument,
because when this touch card gets sent to me,
I need to flip it over. So, I have to talk to it, okay, so
I want that argument. And really important,
pay attention right now if, if you're dozing right now,
is that we do not. This is the type of the arguments.
See it says Type Any? And we want that type to be Button,
okay? Because a button is sending this method to us.
Now I don't know why that doesn't default to Button.
I've been saying that for years, it should. But
it doesn't. And if you don't change that from any to button
the rest of this code is not gonna work. So definitely,
if you remember nothing else from this, remember to change
that. All right? So we have this button here and I'm gonna
hit these other things. But you know, here's obviously,
we were sending it to the view controller.
The event touch up in side just means when you touch
up inside the bounds of the button, send this message.
All right, so let's connect. I'm gonna hit Connect.
It's gonna give me a method.
This is your first look at a Swift method right here.
And let's look at the parts of it. Interestingly,
this is not part of a Swift method, okay. This is just
a special directive that Xcode is putting in there.
That causes it to put this little round circle here where
the line numbers are. You see how line number lucky 13 there
is a circle instead. If I mouse over that circle,
I'm not clicking just mousing over. If I mouse over that
it'll show me which buttons send this message,
which ones invoked this method. So
that's all the IBAction does just cause this circle to
appear here. Now this is the syntax of a Swift method.
So let's look at it's parts.
Func. It's just a key word that says this is a function.
A method is just a function on a class. It is legal to have
functions outside of classes. They're global functions.
We almost never do that cause we're very object oriented,
but you can do it. This is the name of the method,
touch card. I chose that in the little pop up.
This is a list of all its arguments. Okay,
this method has one argument. The type of the argument comes
at the end with a colon, colon type, which is a UIButton,
obviously,. We said we wanted one argument,
which the sender was a UIButton, that's what it is.
This are the names of this parameter.
Names plural. Now, there are two things about Swift that
are going to be quite different from what you're
used to in other languages. One, every argument has a name
that you actually include when you call the method. Okay?
So like in Java you would
say touchguard open parenthesis six comma hello
comma five. You would never do that in Swift.
Each parameter would have a name in front of
it. That way if you're calling it and you're reading
the code you don't have to remember that first argument,
what's that again? Because they're each named. And
the other thing is it has two names. Okay.
The two names are the external name.
That's the name callers use and the internal name. That's
the name we're going to use inside of are implementation.
Now I'm going to write my own method in a second here and
we'll talk about this more, but that's what's going on
here. Now if this method had a return value, you show that by
saying arrow Int lets say that would mean this method
returns an Int. Very simple syntax for that. But
this method doesn't return an Int, but you could make it so
it does. Okay, so let's make sure this is working.
I'm just gonna use that print function I was talk about this
is a global function called print. It just takes a string
and prints on the console. So here I'll print agh! a ghost.
Okay, and we're gonna run our app and
see if this is working when we press the ghost. So
by the way, when we are gonna print stuff from the console,
we probably wanna bring this up from the bottom so
we can see our console, we can make our debugger smaller.
Here's our console bring it up. There's actually a cool
thing you can do, I'll show you a Xcode tip.
If you got here to Xcode, Behaviors and Edit Behaviors.
And you go down you can have it so
that Xcode does things, opens windows,
does other stuff when certain things happen. For example,
if you're running and
it generates some output on the console, you can say
show the debugger. Okay, and it will automatically bring
that up from the bottom if it's not there.
So that's kind of a nice little feature. All right, so
let's run it. Those are the kind of tips and tricks we'll
cover on Friday, by the way. Good reason to come on Friday.
Question, yeah? >> Can you go over again
how you link the button to the view controller?
>> Yeah how we
connect to the button view controller?
Yeah, I'll show you that in just a second.
So let's make sure it's working and
then I'll show you how we connect to it again. So
we have this button here.
I'm gonna click the ghost and look down in our console.
Look, agh! it's a ghost, okay. So it's working. So
we're clicking the button and it's doing that. So
the question is, how did we hook it up again?
Well what we did is that we held down control, the control
key, and we dragged from the button into our code.
And when we let go it asked us all the questions.
What do you want to call it?
That kind of stuff. All right, so we got this hooked up,
right. Now we need to flip the card over when this happens
instead of just saying, agh! a ghost. Okay?
So we're not gonna say agh! a ghost anymore. So
to do that, I'm going to add my own function that flips
the card over. So let's create our own Swift function.
So we have to say func cuz its a method on a function.
I'm gonna call this flipCard, okay.
And I£m gonna have two arguments.
One is the emoji that I want to be on this card,
the ghost or whatever. The other one is the button
that I want to set the emoji on, or whatever.
So, interestingly here, I£m going to call in these things
constrain what you might think are strange names.
I'm gonna say with emoji, emoji of type string. So
this is in external name with emoji internal name emoji. And
how about this one on button UIButton. Okay?
Now, these might seem really strange to you as names of
parameters, both internal and external.
And in your reading assignment on the last page, it's gonna
link you to a document you have to read that explains
how to pick good names here. Cuz there's a very well
defined set of rules for picking good names here. But
the number one rule is when someone calls this function,
it should read like English. That is the number one
requirement of picking these names.
So let's call this function in touch card right here,
cuz we want to call it with the ghost. So I'm gonna say
flipCard. Okay, notice, by the way, as I start to type,
look at Xcode trying to help me. Xcode's so nice,
you see look, it know that I have a flipCard method.
In fact, I'm just gonna hit tab and it shows me the method
with the argument names. And I'm just ready to fill it in.
So this is called, from the caller's point of view,
flipCard(withEmoji). The emoji is let's say, for
example, our ghost. I'll just copy and
paste him from right here. On the button and which button?
Of course, the sender. That's this sender right here. So
you see when I call it, it reads like English.
Flip the card with the emoji ghost on the sender button.
That's our number one thing we're trying to accomplish.
But inside here we wouldn't wanna be saying like, well if
the button title equals with emoji that just is weird.
It's sending a message to the button that says on.whatever,
that would make no sense,
so that's why we have these different names.
Now, it is possible and legal to only have one name like
just emoji. Then the external internal name would both
be emoji. And this underbar thing like right here.
That means there's no argument,
in other words it's just like Java or
some other language. We almost never do that, it is done,
the document will tell you when you can do it,
we almost never do it. It's here in this touch card
because that's an iOS thing that's sending this message.
It's from back in the Objective-C world.
Objective-C doesn't have this internal external name thing.
So that's why it's under bar there, okay, but
we don't really use under bar that much. All right, so
how we're gonna implement this flipCard right here?
Well, this flipCard is essentially gonna toggle it,
and I'm just gonna have this flipCard method,
look at the button If it's already the ghost,
then I'm gonna have it flip it over to orange with no text.
If it's not, then I'm gonna put the ghost on it with white
background. Just exactly what we played around with earlier.
So, we need to check to see if the buttons current title
is this ghost. So I'm just gonna try and type this in
let's see, if the button. Okay, now I need to send
a message to the button asking it what's your current title.
Well, probably I wanna go look in the documentation, figure
out what button does. In fact, that's what you want to do.
But there's actually a cool way if you just wanna guess
what it is. You can press dot. That's how you send a message
in Swift to just like Java, right, dot to send a message.
And when you do that Xcode is gonna show you all the methods
and variables that button understands, and let me say,
there are quite a few. Okay, I'm scrolling through here,
I'm only down to the Cs okay, we got Ds,
there's probably a couple hundred. So
how does this help me? This doesn't help me at all.
Well, the cool thing is, if I just type a word here that I
think might be in the name of the method I want like title.
I want you to set the title. Now it just shows me things
that start with title, or that have title in it, or
have T-I-T-L-E in it, which is not very useful, but
that's why it puts those at the bottom. But anyway,
we got the title. So now I can start looking around in
a much shorter list here, to see if I can find it.
So here's title color, definitely I don't want that.
Title rectangle, no. Here's set title.
I don't wanna set the title right now but
that's kinda good to know. Look at this one, current
title. Current title that is displayed on the button.
Victory, I found exactly what I want. And when I find what I
want here, looks like by the way, returns a string?
We'll try it anyway. If I double click on it,
it's gonna put that there for me. And now I can just say
is that equal to the emoji that you're asking me to flip
the card to. This first argument. Notice how I'm using
emoji and button as my internal names here.
I'm not using with emoji or on. That was for the caller.
Okay, so now I found out that the button already here
has the ghost on it. So now I wanna make it orange and
blank. So now I need to set the button's title, and I saw
before that it was set title. Here it is, set title. Now
this set title has extra thing here, for UI control state.
You see that? It's like, what the heck is that?
I don't know anything about buttons, so, I'm not sure.
But I don't see any other set title, so I guess I'll have to
go with this one. Okay, so I double click on it. The title
I want to set here, of course, is empty string because,
I'm trying to do the back of the card here.
So I empty it out. And now I'm kind of at this control state
impasse, because I don't really know what this means.
So, here's another cool thing you can do,
is hold down option and mouse over anything,
you see how it's highlighting things with a little question
mark right there? And you can click on it and it'll show you
the documentation for that thing. So here is set title,
the documentation for it. And if I read through this,
the third paragraph and description says,
at a minimum, you should set the value for
the normal state. Okay, sounds good. Now I'd like to
understand how to get that normal state, so I'm gonna
actually click on this link here, you see UIControlState?
And it's gonna take me to the documentation and
show me control state. So here we go, click brings up
the documentation, here's control state, see that? And
here it is normal, the very first one is called normal.
And we're gonna see later how we use these things but
this is a static, you see it's a static bar.
So it's kind of a property of the type, and so
the way that we will type that in is to say,
UIControlState.normal. So it's awesome we found that, but
while I'm here in the documentation,
let me briefly show you how the documentation works.
Right here, you can see that I'm looking in UI kits
at its views and controls at the class UIControl, which
button inherits from. And we're looking at this thing,
UIControlState. But I can click anywhere here and
go back and maybe look at UIButton if I want. And here's
UIButton. Now, when you're looking at the documentation,
I super strongly recommend you read these overview sections.
You see where this says Overview?
This overview section in each class,
only take you five minutes to read. And it's really
gonna help you understand what the heck is going on here.
So, I strongly recommend you do that for
all the common classes that you're using, UIButton later
we're gonna do array in dictionary. Go read it so
you understand what the heck is going on in here. Got that?
You can also search the documentation of course here.
And in addition to overview it has a list of all the methods.
And here, for example,
here is set title again. Go back and see if it's the same
that shows up in that little box. So now we know that this
control state here is supposed too be UIControlState.normal.
It's gonna set the normal control state. We also want to
set the background color so I'm going to do button dot and
I'm gonna just take a flyer, backgroundcolor.
Sure enough, look backgroundColor or something,
exactly called backgroundColor that takes a UI color,
I think? Let's double click that one. And
I'm gonna set that equal to, believe it or not you can
actually put a color literal in here. You just
do that by starting to type the word color, and you're
gonna see the first choice is always Color Literal.
If you double click on that,
it puts a little square in here.
If you click on the square you can pick the color you want.
So let's go back and pick our nice orange,
cuz that's what we do in the background.
And so, we've set our background color to orange.
And it's really nice to be able to see that in your code.
It really highlights the colors you're using.
All right otherwise if it's not, doesn't have the ghost
right now then we want to put the ghost on there.
So I'm gonna do pretty much exactly the same code here.
It's just that I'm gonna set the title instead of blank,
like the background. I'm gonna set it to the emoji,
the ghost in this case. And I'm gonna double click on this
orange square and change it to white, because I want a white
background for the front. We got that? Okay,
let's run, see if our card. Our card should flip over now,
which is getting us quite a long way along the path here
to getting concentration working. Right here is
our ghost. Cross your fingers. Whoo, it flips over and
back. Okay, excellent. All right, we're on a roll.
Now, let's go and
add another card. Okay, we got one card. Let's go to
the second card. Believe it or not, you can take things in
Interface Builder that are the way you want it and
just copy and paste them. So now I have two cards and
this card already is the right shape that I want,
it's got the right font size all that stuff.
So it's really recommended to copy and paste verses dragging
out a new one and trying to set it to all the same thing.
Just copy and paste.
Now this one wants a different thing on it, so let's do
something like pumpkin. Yeah, well we could do a pump and
now we'll do pumpkin, all right? So we got pumpkin.
So we had pumpkin card and we got a ghost card.
Now this pumpkin card, it needs a method.
So we're gonna Ctrl+drag to create another method.
So if you missed it the first time, this is how we do it.
It's an action. I'm gonna call this touchSecondCard.
The type, of course, we want a UI button, argument sender.
Connect it, here it is. I'm gonna take this
exact same code from here, put it down here,
but instead of the ghost right here, I'm gonna use R pumpkin.
Okay, looks good, right? This could not possibly go wrong,
this is so simple. It's gotta work,
okay, let's do it. All right, here we go,
the ghost still working like a charm. And a Pumpkin, pumpkin
not working. Okay, what is wrong with our pumpkin?
How could this pumpkin possibly not be right. Well,
we could get in the debugger here, set a breakpoint,
it's real easy to set a breakpoint by the way.
You just click on a line number,
puts the breakpoint there. But
we're gonna do a little more lightweight debugging.
I'm just going to put a print here in flipCard, to see if
this is even calling flipCard. It should be calling flipCard,
because it's connected here to this method, so it should be
calling flipCard. So I'm just gonna go in here and
say print, that I'm in flipCard withEmoji. And
now I wanna put this emoji in here. Now,
in a lot of other languages, you would probably say %s,
emoji, or something like that, right?
This would be your, oops, this would be your string.
But we don't have this %s business in Swift,
we have something better, which is\(). So
\() means put something inside there, it's gonna interpret it
as a string, and embed it in this string. So that's exactly
what we want, emoji, and the cool thing about it is,
I could put, this is a string, so it's easy to embed.
But I could put an Int here, anything that can be converted
to a string, which most things in Swift can, I can put there,
I can even put an array there, or a dictionary.
As long as the array only had things that could
be converted to a string, then it can convert the whole
array to a string. So this is a really cool feature,
this\(), all right? So let's run again, and
see if that pumpkin is calling flipCard, which it should be.
All right, so the ghost, it's definitely calling it,
see flipCard withEmoji, the ghost, how about the pumpkin?
It looks like it's calling it, but I didn't press that twice,
what's going on there? That that's kinda weird,
let's do it again. No look at that, it's doing both.
Every time I press the pumpkin,
it's doing the ghost and the pumpkin, ghost and
pumpkin. Why is it doing it twice, okay,
that's clearly they're messing things up badly.
Well, let's look and see why it's doing that. We know that
this method is being called by the pumpkin. And
this method is being called by both. that's the problem.
And why did that happen, cuz I copied and pasted the ghost.
And when I copied and pasted the ghost, it copied and
pasted the fact that it was sending that message. So
this is a common mistake to make,
really easy. So I did it intentionally to show you how
to get out of this, which is to right-click on something.
If you right-click on anything in the UI, it will show you
all the connections that it has. So this is the pumpkin,
it's sending touchCard and touchSecondCard, but
we only want it to send touchSecondCard. So
I'm gonna disconnect touchCard by clicking right here.
Now I fixed it, okay, now the pumpkin is sending this,
and only the ghost is sending this. So right-click is
an important thing to know, if you ever get in a situation
where it seems like,
I thought I wired that up, why is it not sending it?
Or even why is it crashing trying to send something that
I didn't intend? Right-click on it, will tell you why.
So let's run, see if that indeed fixed our problem here,
I imagine it will. Okay, it's got the ghost flipping over,
we got the pumpkin flipping over, okay, excellent,
all going great. Now before I add yet more cards, I want to
have some UI that tells me how many times I've flipped cards.
Because you notice in this game,
the fewer times I flip it, the better I am at it, right?
If I flipped them all over, up and down,
up and down, then I'm not very good, it's easy,
I didn't have to concentrate. So I wanna have some UI that
says how many times I've flipped a card, okay? So to do
that I'm gonna start, let's get rid of this print in here,
we don't need that debugging anymore. So I'm gonna
start by adding an instance variable to my class that
keeps track of the flip count, okay, real, real simple like.
Move this down here so we can get some space, all right?
So what does it look like to add an instance variable to
your class? Really simple, you use the keyword var, short for
variable. Then the name flipCount, then the type,
: Int, that's it. So I have added here a variable called
flipCount to my class, and I£m gonna keep that flipCount up
to date as these cards are flipped on. But,
look what happened here, I had an error. So this red, when
you have a red error, your app won't even compile and run.
You can also get a yellow one, then your app will compile and
run, but do not submit your homework with yellow ones
either. Even though they're just warnings,
they are usually indicator something bad, so
don't do that either. But this red, I gotta fix this red.
Now what's funny is this red came up on a different line of
code. I typed this line of code in, and it came up on
a completely different line. Well what does this say here,
it says, class ViewController has no initializers.
Why is it saying that, what does that have to do with it?
Well, this is happening because Swift requires
all instance variables, which by the way in Swift,
we call instance variables properties.
So when you hear me saying property,
I mean instance variable. So all instance variables,
all properties have to be initialized,
you are not allowed to have them like this flipCount int.
What is the value right now? It has no value, okay,
it has to have an initial value,
it's just not allowed to be sitting out there. Now there's
two ways to initialize an instance variable,
one is using an initializer. So that's what this error is
trying to tell us, get an initializer here, dude.
An initializer is just this method with a special name,
init, short for initializer, and
it can have any arguments you want. In fact,
you can have multiple inits, each with different arguments.
But each init is responsible for initializing
all of the variables, okay, that's what the init's job is.
So we could add an initializer here and do that.
Now, unfortunately, adding an initializer to a class can be
kinda complicated because of inheritance, right?
Because your superclass has initializers,
you gotta make sure they get called properly.
So I'm not even gonna talk about initializers yet, okay,
I'll talk a little bit about them on Wednesday. Next week
we'll really talk more in depth about initializers. And
so what's the second way that I can initialize this,
since I don't wanna do initializers right now?
And that's just right here to say, = 0, okay,
if I just, why do I keep pressing v there, okay,
Int = 0, okay. That initializes this,
that's gonna cause this error to go away, all is well. And
this is how we initialize most of our instance variables.
Now while I'm here, talking about instance variables,
I wanna talk a little bit about Swift and typing.
Swift is an extremely strongly typed language,
I mean extreme. It does have a type which is kind of like
untyped, but really it's mostly for
backwards compatibility with Objective-C. In most cases you
want to be very specific about what types you're using,
and some people complain about strongly typed languages. It's
so much burden to always be typing something. Okay, well
Swift is also a language that has strong type inference,
which means it will, if it can, guess the type for you.
So in this case, you see I have this : Int type,
that is completely unnecessary in Swift. And
not only unnecessary, we would not put it in there, there is
no reason to put that there. Why is that, because this 0 is
clearly an Int. Swift treats all literals like this,
that don't have any decimal points as Ints. And
so this is an Int, and how do we know that? If we hold down
Option, just like we did when we clicked on Title down here
to get the documentation, we can click on flipCount, and
it'll say flipCount is an Int. You see that, and if I
had said flipCount was 0.0, then if I hold down option and
go back here to flipCount, it says flipCount is a double,
okay? So into, it infers it, if I said flipCount equals
"hello", it's going to say okay,
flipCount is a string All right, so it's gonna infer.
Now, this is easy to infer cause this is a constant but
swift can even infer in the most amazing complicated
situations. There's only one type that thing can be,
it'll make it that type and
you don't have to put the types.
So it's surprising in Swift. The time you put types in
the most is arguments to functions. Because that you
kind of have to say what, what you're expecting there. But
otherwise you rarely are actually putting the types.
It's quite amazing, actually. All right, so we have this
flip count here. Let's go ahead and increment it. K,
every time someone steps, presses on a card I'm gonna
say flip count plus equals one and ug I'm gonna copy and
paste the code. God. Anytime you're copying and
pasting code, you're doing it wrong.
Okay, that can't be right. So I just did something that's
not right but I'm doing it anyway. I could probably put
that somewhere else but I'm gonna just.
We'll do it this way for now. You'll see why in a second and
we'll fix it. But we don't want to complicate the code.
But each time a card is touched,
I'm gonna increment the flip count. Now that's great, but
I need this flip count to appear in the UI,
okay. I want the user to see how many flips they've made.
So I have to have this int
appear in the UI. So how do I do that? Well,
I need some sort of other UI element besides a button And
that's just a little text field. Okay, in fact there's
a read-only text field which is called the UILabel in iOS.
And so of course anytime we wanna add something to UI,
I go here, to the bottom side, in fact right next to button,
here is label. I'll drag it out and put it in here.
I'll try and center it or something like that.
It's really really small so I'm gonna make it much bigger
cuz I'm gonna be using big font in a second here.
Even make it taller. The text is black so
it's black on black. That's a little hard to see so
I'm gonna go back over to here, go to the top half here,
change the color of the text, okay,
which is a label property right here. From this,
to our favorite color, orange. This text is really small, so
I'm going to change the font from 17pt,
which is the default here, to something like 40.
Much better. I don't want it to say label here,
I want it to say Flips, let's say we'll start out Flips: 0.
I don't want it left-aligned like that, okay,
I don't want the text left-aligned,
I want it in the center, so I go over here and hit center.
So see how I'm just using this editing inspecting up here to
kinda get the thing to look the way I want it to look.
I can even use my dashed blue lines here to
put it right along the bottom and right in the center. Okay.
Even though I said we are not really using those,
it£s still useful to do that one.
>> All right, so
now I have my UI, how do I talk to it? How do I tell it
when the flips change to show the flips? Well, I£m gonna
do that also by making connections between my UI and
my code. And we know how to do that, Ctrl drag Right?
Ctrl+Drag, put it right here. And this time,
though, I'm not doing an action, I'm doing an outlet.
And what an outlet does is it creates an instance variable.
So action creates a method,
Outlet creates an instance variable or property and
that property is gonna point to this UI label and
I'll be able to talk to it and
tell it to update itself when the flips change. All right,
so what am I gonna call the name of this thing, I'll
call this my flipCountLabel, it got the type right here
didn't see any. Don't worry about this storage week.
I'm gonna talk about that next week. And so
I'm gonna hit connect and it's gonna create another var.
Like var flipCount. But this var is gonna be a little
different. This var is, sorry wrong place there.
This var is var flipCountLabel: UILabel!
Okay? Now you can't infer this type by the way because
that UI enables in the UI. So it doesn't know how to look in
the UI and infer a label so it can't infer that there.
So we do need to explicitly say it to UI label here. Week,
I told you to look, we'll tell you next week.
This is another kind of directive like this one that
puts the little dot there so that we can see that's that,
that's that, that's that. Okay, so here's the var,
exactly the same as we did with flip count.
Notice there's a little exclamation point there. That
is super important. But I'm not gonna talk about it today.
I can't talk about everything all at once. Notice that this
very important thing also has a side effect.
It's not the primary purpose of it but
the side effect is. It doesn't have to be initialized.
notice this doesn't say equals anything and yet
we don't have an error. Okay? So don't freak out about that.
I'm gonna talk all about this exclamation point.
It could not be more important,
probably the most important newish
thing that you're going to learn about Swift, okay,
is what its all about. All right, so I got this flip
count label right here. Now, all I want to do is,
every time this flip count changes, like, right here.
I'm going to talk to the flip count label and I want to set
it's text, label. I want to set it's text, so I'm going to
send it a message and I want to set text, so I'm just going
to say text. There happens to be something called text,
the very first one there. You see that?
It says text as a string, so I'm going to double click on
that All right and I'm gonna set that text to, Flips:,
and then I'm gonna use my favorite feature right here,
\(). And I'm gonna put the flipCount in there. So
that's gonna change the text on this flipCount layer here,
and we'll say Flips: 1, so it's going to whatever And
of course, I have to copy and paste again right
there because I need to update it each time. So,
let's see if
this works.
Okay, here we go. Let's try to flipping this card. Ooh, yeah.
Everytime we flipped over our back.
It's incrementing the flip count. Okay, fantastic.
Except this really is nasty. I just copy and pasted.
I mean, imagine I really was doing it this way and
I had another button called New Game and
I had to set the flip count back to zero.
I'm going to have to put this same line of code there to.
And what if some day I said, well,
I don't want this to say flip, I want it to say flip count.
Now I've got to change it all three places.
This is just horrendous coding.
Luckily, in Swift, there is a way to avoid doing this in
this case, which is that any property, if you want,
you can put code after it that says, did set. And it will
execute this code every time that thing gets set. Okay?
That's called a property observer. Because this code is
observing changes of this. So we can take this outta here,
put it up here, and we can take it away from here now.
Every time flip count changes, it's going to Execute that
didSet, and talk to the label, and update it.
So now if I add a new game asset, flip count equals zero,
it would automatically update it.
Property observer is really cool.
Obviously we use property observers a lot to keep the UI
in sync with te instance variables of our class. Okay,
so you'll see that happening all the time. All right, time
to get more buttons. We got these two buttons. By the way,
notice that we're driving our UI from the code, right?
It's this line of code that's saying what's in the UI.
So I'm actually gonna change these two buttons to be
face down.
Okay when they start. And what's interesting is if I
select them both, and go over to the top here of this thing,
notice it notices they're both buttons. So
I can change things, like here it says multiple values for
the text, ghost and the pumpkin.
I can set that to blank, and I can go down to the bottom and
set the background color here to orange And it's affecting
both of them. So if you select multiple things,
as long as they're of the same, they share the same
type, then you can edit them all simultaneously.
Okay. So I have these cards, let's make some more cards.
I'm going to copy and paste these cards. Actually,
before I do that, let's do one other thing.
I'm pretty unhappy with my architecture here,
because if I wanna add more cards I guess I'm gonna have
to do copy, touch third card, fourth card, fifth card,
sixth card, seventh card, eight card, nine, okay.
That is gonna be the worst code, you would get an F on
your homework if you did that. That's just awful, so we're
clearly not gonna use that architecture to do that and
in fact I want to get rid of this touch second card
entirely, and I just want to put everything in touch card.
And that means I won't have this line of code duplicated
and this line of code duplicated.
This is gonna be great. This is much better, UI.
So to do this I'm gonna right click on this one and
disconnect touch second card and instead have this guy
ctrl-drag to here. Look at that you can hook it up to one
that already exist by just dragging to it and
letting go. So now both of these are sending Touchcard.
Okay, so that's great but inside here we
obviously can't do this. Have this ghost thing here,
because then it would set both cards to ghost. So how are we
gonna have code in here in touch card, that works for
all the cards? Well what I'm gonna do is I'm gonna create
an array of all these cards and when touchCard is pressed,
I'm gonna look in that array, find the button that's being
pressed, and then I'll know which card it is. Then, when I
know which card it is, which index it is in the array.
I'm going to lookup in another array, the emoji to put there.
So, it's going to be data driven. Right, and so
if I want to add more cards,
I just add more cards to my emoji array.
It's going to be great. All right, so
let's make more cards. I'm going to copy and
paste to make more cards, so I have four cards and
now I wanna make an array that includes these cards, okay.
So how do I do an array that include these cards?
Well, that's a connection between the UI and the codes.
So I'm gonna control, drag and create another var right here.
Okay, but this one's gonna be the third kind which is outlet
collection. So now outlet collection means an array
of the things in the UI. So I'll call this myCardButons,
I'm actually gonna make a mistake, okay and
say card butons instead of buttons,
because I want to show you how to fix this mistake if you do.
And it's got the type right, this is gonna be an array of
UI button. Okay, so I'm gonna connect it right here and
it created another var for me right here.
This one and look at its type. See that right there. That is
special syntax in Swift that means an array of UI button.
This might look more familiar if I use another Swift syntax,
array of UI button, that look familiar to
you from Java, right? So array is a generic class, everyone
know about generics in Java, hopefully. It means that
you can't have the class array on its own be a type in Swift,
because I told you Swift is very strict about types.
So when you are putting things in and out of that array,
Swift would have a heart attack if it
didn't know the type of thing you're putting in there. So
when you create an array you have to specify what type of
thing is in the array so that Swift can breathe easy, okay.
Now arrays are so common that instead of using this
normal Swift, this is normal Swift syntax right here, but
for arrays, we do this special thing here, where
we say [UIButton]. It's just for arrays that we do this,
dictionaries also have a special one, I'll show you
that on Wednesday. Okay? So that's our array,
CardButons is array, now I have this cardButons here
okay, and this is connected to that one card, I haven't put
the other cards in there yet but I will. It's connected to.
Now what if I decided no, I don't want Butons,
I made a mistake, it's Buttons and remember I told you,
you don't wanna edit things in here then also in the UI,
okay like the name of the class and things like that.
Well if you do that look what happened to the little circle.
Okay it doesn't have a dot in it anymore because
it's not connected anymore.
Because this, if I right click on it, it's connected to
Card Butons there, okay? It's not connected to Card Buttons.
So, how can I fix this? If I said this Card Butons,
okay it's back to being connected now and
I want to change this. It's another magic key.
The command key. We all ready showed you the alt key for
getting into the documentation.
If you command click on something, k,
like this cardButons right here.
You get another kind of menu here that'll do some cool
things like jump to the definition of this thing,
which happens to be right here so
that's not gonna go anywhere. Show the health,
just like the option one does, and also rename and
when I click rename here, look at what happens.
The UI kinda turns a little bit. It looks through my
entire project and finds that cardButons everywhere
including in my storyboard. See that? So now if I change
this to cardButtons here, it changes it everywhere and
now, this is not broken. And if I right-click on this,
it cardButtons. So command click rename. That's how you
want to rename things that touch both your UI and
your code. Right, so I've got this card buttons here. Now,
I need to somehow in touch card, I need to
look into my card buttons array and find that button.
Okay? By the way, before we do that, let me put the rest of
these cards in there. I'm going to show you another way
to connect up your UI to your code. It's this yellow button
right here. You see this yellow button at the top?
It says view controller. This button also represents your
code. So, if you want to control drag from here,
you can do that as well and hook up to card buttons.
Control drag from here. CardButtons and I'm
intentionally not gonna hook up this card cuz I'm gonna
show you what happens when we don't hook up a card. So
look at cardButtons, got those three cards and not this one,
right. All right, so how am I gonna look in cardButtons for
mine? Well of course Swift array is a fantastic class.
It's got so much great functionality and
one thing it knows how to do. Is look inside and
tell you what the index is of something inside of it and
that method is called index of all right. So let's just call
that method, I'm gonna create a var called card number
which I want to be the number the card 0,1,2,3,4 wherever it
is in the array. I'm gonna set that equal to cardButtons dot,
right sending a message card button index. Now interesting,
look how many methods are called index in array,
multiple methods. In Swift, that's perfectly legal. Swift,
you can have 100 methods all with the same index, but they
all have to have different arguments. You see how all of
these index methods have different arguments?
All right, and they get the index after something,
index before something whatever.
I want this one down here index of something which is
returns a int, okay, that's gonna be a problem that int,
we're gonna see in a second here but
if I double click that, I've got the index here. So,
I wanna find the index of the sender cuz I'm in touch card
right here, so this sender and I'm just gonna print this out.
So I'm gonna say card number equals and
we'll do our magic thing here, and
we will just print out the card number, okay.
So before we go look in up in an array and find the emotion,
let's just make sure that we're finding the right card
right here. Now, right away I get two warnings here.
Hm, let's go look at these warnings in depth.
Let's make our whole screen show these warnings.
The first one is Variable cardNumber was never mutated.
Consider changing to let constant.
What does that mean? Well indeed, cardNumber,
we give it an initial value here of looking up the card
in the card buttons array but we never change it again.
So it is not back in fact variable. It's a constant.
Okay, and in Swift we always mark constants as constants.
But we don't use const like in C and
other languages, we use let, L-E-T.
And here's another cool thing. You see this little triangle?
If I click on it, oftentimes Swift will fix it for me.
You see that where it says fix. It's saying,
you want me to replace var with let? Like yeah, sure, and
it did, see? Now why do we use let instead of const?
Because we want Swift to read like English.
let cardNumber = cardButtons.index of
the sender, sounds like English to me, ish, okay? So
that's better than const cardNumber =.
So you're gonna get used to using let, always use let when
something's a constant, never use var, all right?
This other warning right here, I'm gonna ignore. And
let's just see what happens here when we run.
So let's run this guy in our simulator, we're ignoring
that, pay no attention to the yellow warning there.
Okay, here it is, let's click some of these buttons and
see what happens. See, it's going to print.
We'll put this over on the side so you can see the code
at the same time. We're just gonna print out the cardNumber
that we found by looking that index up.
Ready, click. That's kind of weird, look at that,
Optional(1), Optional(2), what's that Optional about?
There seems to be seeing the card right, right, 0, 1, 2,
but it's saying Optional, why is it saying Optional? Well,
this is that super important thing I was telling you about?
Here it is. Lets option-click on index right here, and
go look at it's documentation. And
the return value of index is not an int,
the return value of index is an optional,
that's what that question mark means.
We've seen question marks all over in the documentation,
right? It means optional. And optional is a different
type entirely from int. Okay, has nothing to do with int.
Optional is a type that has two and only two states,
set and not set. It's an enumeration.
You probably seen enumerations in other languages,
enumerations are things that have discreet set of values.
This one only has two values, set and not set,
that's it. But the cool things about enumerations in Swift,
not a lot of other languages have this. For
each case of an enumeration you can have associated data,
just data that goes along with that thing. Well, an optional,
when it's in the set state, has associated data,
which in this case is an int. So this index method is
returning whether or not it could find that button in
there by returning set or not set. And if it does find it
it also gives you the associated data of the int.
That's what you're seeing down here. It's printing out,
when we print cardNumber it says this is an optional.
It's in the set case and
the associated value is an int. Got that? Now what if we
click on the button that's not in the cardButton's array?
Let's see what that prints, that does right here. Oops,
we stopped, let's go back and run it again. I'm gonna click
on that fourth button that is an in cardButton.
So when index looks it up it's not gonna find it.
It's gonna return not set.
Let's see what that looks like? Click nil, N-I-L. So in
Swift the word nil means the not set case of an optional.
That's the only thing it means. In other languages,
it means zero pointer and other things. No, in Swift it
always means an optional that's not set. Got that? All
right, so that's no good to me though, to have an optional.
I can't look that optional up in another array of emoji,
I need the int. So how do I get that associated value in
the set state? Well, one way is to put
an exclamation point at the end of it. If you put
an exclamation point at the end of an optional, it says,
assume this optional is set, and grab the associated value.
So let's see what that looks like. You're gonna see
this optional syntax is all really simple question mark,
exclamation point, one characters, because it's so
common to do these optionals. All right, so
here look, cardNumber = 0, 1, 2, excellent,
it's working. What do you think happens if I press this
one right here? >> Crash.
exactly. Why is it gonna crash? Because you're
>> Crash,
returning an option that's not in the set state, so we don't
have that associated value, so it crashes your program.
And you're gonna see this crash quite a bit, okay,
when you're developing because you'll just accidentally do
this quite a bit of time. And if you look in your console,
look what it says. It says an error here,
fatal error unexpectedly found nil, that is to say not set
while unwrapping an optional. Get used to that error,
you see it all the time. Now, this might make you say, whoa,
I'm never using exclamation points,
gonna crash my program, scary. Yeah,
it's scary, but crashing your program can be really good,
because it makes you find problems.
Like in this case, that fourth button is supposed to be in
card buttons It's a bug that is not in card buttons. And
if I didn't crash right there, I might not find that bug,
cuz I might not have clicked on that one,
I might not have noticed. This way, I'm for sure gonna find,
it's gonna crash. And now it's gonna crash, it's gonna crash
right where it matters, right in the debugger so
I can look at it. So don't be afraid of crashes, okay?
Crashes can be good. But let's say you wanted to do this
code in a way that didn't crash,
that kind of conditionally looked to see if it was in
the set state.
And if it was, then use it, otherwise didn't do it.
To do that, instead of putting exclamation point at the end,
you take that off and put if, at the beginning, okay?
And so now, if this optional right here is in the set state
then this code will execute, otherwise it won't crash. So
that's if that's conditional, you can see again,
optional minimum possible syntax here.
So now, we can put this print cardNumber inside here,
and we could even, if we wanted to, put an else.
Could something like chosen card was not in card buttons,
or something like that, so we could notice that it happened.
And here we're conditionally doing it, so
let's go look at this I'm gonna move,
make this console wide here so you can see what's going on,
all right? So here this is still working cardNumber 0, 1,
2 because I'm using that if let right there. But now,
if I click on this bad button it just says chosen card was
not in card buttons, right?
It says this, okay? So that's optional is how to unwrap
them. Super duper important. Really gonna have master this.
I know that's new to you. We're gonna talk about
these exclamation points up here, that I mentioned before.
That made it so you didn't have to initialize your thing,
those are optionals as well. They're a little different
kind of optional because their exclamation point instead of
question marks. Remember this index one right here?
Okay, that was a question mark one. These are exclamation
what one's they're slightly different optionals but
they're still optional. We'll talk about that later.
So the last thing I wanna do here is just take this
cardNumber that I got, and
go look it up in an array of emojis. So I'm just gonna
make a var, I'm gonna call it emojiChoices. It's going to be
an Array of String emoji strings basically.
And you can create arrays right on a fly by doing open
square bracket and just put in the things in the arrays.
So I'm gonna go put my emoji in here, lets go back to our
emoji chooser, probably its in frequently used.
Here we go, there's the [INAUDIBLE] for
the pumpkin there, I'll put a pumpkin here.
We'll put a ghost here, put a ghost over here,
all right? Now we've got that, so we're gonna look
the cardNumber it's gonna be index 0, 1, 2, 3.
We're gonna look it up in here. While we're here let's
go ahead and wire up that last wayward button that's not in
the cardButtons array.
I'm gonna do that again from up here, Ctrl + drag, down to
here to put all four buttons in there. Go back to Automatic
here, and one other thing I'm gonna do is note that this,
is that necessary? No, because it's clear that
this is an array of strings. So if I option click on this,
on emojiChoices right here, it says array of strings.
So that again, the inferring. We would never put that colon
array string there, never. Totally unnecessary. Okay, so
now let's say just printing the cardNumber out right here,
I'm going to call my flipCard. FlipCard with emoji and
the emoji is gonna be emojiChoices sub cardNumber,
okay, and it's on the sender. Got it, let's run it.
Okay, here we go. Pumpkin, ghost.
All these considered working.
Now, we might be very proud of ourselves here because we got
this data-driven architecture. It's really great
we can add more buttons now. We can make 20 buttons and
all we gotta do is add more things to this array.
But this is actually not a very good architecture,
because this is very fragile, okay. The number of emoji here
has to exactly match the number of buttons in the UI,
and you have to put them in there exactly twice.
And they're not in random order,
so the cards are always gonna be in the same place kinda
not a very good solution.
So we really need to take another step and
have a real concentration engine driving our app, it's
doing not only all of this, but all this matching and
all that stuff. Now to do that we're gonna use this design
paragraph dime I talked about the very beginning model view
controller. So I'm gonna start Wednesday's lecture with some
slides explaining how model view controller works.
Then we're gonna apply model view controller here, and
you're gonna see wow, that is a way better way, to do this.
The much more flexible and
extensible easy to add cards not gonna crash everything. So
I'll see you on Wednesday. >> For
more, please visit us at standford.edu.

For more infomation >> Kalyan matka market monday single jodi. - Duration: 2:16. 

For more infomation >> ಕಟ್ಟಡ ನಿರ್ಮಾನಧಲ್ಲಿ ಕಂಡ್ಡುಬಂಧಾ ಒನ್ದು ವಿಚಿತ್ರಾವಾಧ ಪ್ರಾಣಿ - Duration: 1:26.
For more infomation >> AJ+ | Rohingya Refugees
For more infomation >> LES SECRETS DE COCO 💀 - Duration: 15:46. 

For more infomation >> ONPC : Pour Yann Moix, Carla Bruni est "le diable" - Duration: 2:27.
For more infomation >> Eddy Mitchell, une petite visite à Johnny Hallyday durant son hospitalisation - Duration: 1:33.
For more infomation >> SORRENTO, ITALY. | Travel Diary (sott ita) - Duration: 11:36.
For more infomation >> ONPC : Pour Yann Moix, Carla Bruni est "le diable" (Vidéo) - Duration: 2:27.
For more infomation >> Por qué Interior ha convertido a Trapero en un simple oficinista - Duration: 3:36.
No comments:
Post a Comment