Advertisement
English transcript
>> Stanford University.
[ Pause ]
>> All right.
Well, welcome to lecture four, then, of CS 193P for fall
of 2013/14 -- academic year 2013/14.
Today I'm going to do pretty much all slides.
I do have one brief time when we'll stop and do a little demo.
And then at the beginning of next lecture,
I'll be doing a demo that pretty much demonstrates the vast
majority of what I talk about in the slides today.
And remember, that's kind of how it's always going to go:
I'll show it to you in slides, we'll talk about it,
then I'll demo it to you.
Because when you see a demo, it all makes a lot more sense
than if someone's just talking
in an abstract conceptually about an API.
And then your homework is usually going
to ask you to do this.
So it's, you know, you get a lot out of paying attention
in the lectures because I'm pretty much going
to give you hints and basically tell you a lot of how
to do the homework -- at least from the iOS side of it.
I'm still going to ask you to do the thinking side of it,
the designing side of it.
So today we're going to start off by talking
about a little bit more miscellaneous Objective-C.
And then I'm going to go into talking about Foundation,
which is this framework that has arrays,
and dictionaries, and all that in it.
And we'll start to transition out of Foundation at the end
of that and start talking about things like fonts and colors,
which are obviously not in Foundation
because Foundation is really not a UI-based thing;
it's for building both your model,
and your controller, and your view.
But it's, you know, a layer below the user interface layer.
So let's dive right in.
Let's talk about creating objects.
A few of these things are pretty obvious I hope, but I just want
to put them on slides so that you have them in writing here.
You know that most of the time when we create objects
in the heap, we do it with this alloc init business, right?
So sometimes the init is just init
like NSMutableArray alloc init,
and sometimes it's more complicated init like we had
with our card matching game --
init with card count using deck -- so that you're used to that.
And you've also seen us create objects
in the heap using class methods like string with format.
Remember, we did NSString, string with format.
And that's a class method, right?
Because it's open square bracket, name of a class,
and then class method.
So it gets kind of two different ways you've seen already
to create objects.
Sometimes for some methods,
there's both an init and a class method.
So string with format is an example that there's alloc init
with format and there's NSString, string with format.
They do exactly the same thing.
Both of them are there for kind of historical reasons.
Back before we had this automatic reference counting --
you know, the strong and weak thing --
you had to be much more explicit about things
when you were going to, you know, reference them,
and when they got dereferenced, and when you cleared them
out of the heap, and all that stuff.
And one of the ways that was done was using these class
methods to create the string in a way
that it would get released in certain ways.
So all of that doesn't matter anymore.
So now we're kind of stuck with having both of these things.
It seems to me that iOS, the designers at Apple --
I don't work at Apple, by the way.
Some people have asked me if I work at Apple;
I don't work at Apple.
So I'm just seeing iOS from the outside just like you are.
It looks to me like iOS people are moving towards alloc init,
okay, and kind of away from the class methods.
Although I still see the class methods coming
up in certain circumstances, too.
And there's probably some reasoning behind it.
I haven't seen it put in writing by Apple as to
when they choose which, but you're certainly safe
in this class until they start marketing some of these things
as deprecated, like, don't use them anymore,
to use either form, okay?
I tend to try to go for the alloc inits most of the time,
unless there isn't an alloc init that does what I want;
then I'll use a class method.
But there's other ways to create objects.
Okay? You can create objects by asking another object
to create an object for you.
So that's like string by appending string.
That's not a class method in string;
that's an instance method.
You send it to another string, you give it a second string,
and it will create a new string for you
since strings are immutable.
NSString is immutable.
It creates a new string that is the string you sent it
to with the one you passed as an argument added on, right?
So there it's creating a new object for you, okay?
And a very interesting instance method
that you can ask an object to do for you is mutable copy, okay?
So mutable copy makes a copy of an object but it's mutable.
So if you had an NSString and you send it mutable copy,
you'll get back an NSMutableString.
Or if you have an NSArray and you send it a mutable copy,
you'll get an NSMutableArray.
So that's another way that that happens
that you're creating objects.
Of course, not all methods
that return an object are creating an object like we saw
in NSArray -- last object, and first object,
and object date index.
Those aren't creating new objects;
they're just giving you pointers to the objects
that are in those array.
Okay? That should be completely obvious to you but just want
to make it clear, you know, these are pointers to objects.
And you're allowed to have multiple pointers.
And this is normal pointers here being passed around.
Okay? So basically, unless a method has the word "copy"
in it, then if it's returning to you an object
that exists somewhere else, it's going to be returning a pointer
to that object, okay, unless it has the word "copy" in it like
"mutable copy" or "copy."
And if the object that you're getting back doesn't exist
somewhere, then it's going to make a new one for you.
All right.
That's it on creating objects.
Pretty straightforward there.
Okay. Nil, I already talked about this,
that we can send messages through a pointer that is nil,
i.e. that pointer does not point to anything.
And no code gets executed.
So if I do this init I equals obj method
which returns an init, no code gets executed to execute method
which returns an init; however,
that message sending square bracket thing will return zero.
Okay? So that's important to know.
And zero is also what nil is.
So if you had a method that returned an object and you sent
that message to nil, then you get back nil.
Okay? And we can use that, actually, to our advantage.
For example, in our thing we did on Monday,
we did draw a random card, right?
Self.deck draw a random card.
And that returned nil if the deck was out of cards.
Okay? If the deck itself had been nil,
that also would return nil.
So we can write one if that deals with the fact
that we can't get another card, whether it's
because the deck is empty or because there is no deck
because the deck is nil.
Either way, the code's going to look exactly the same.
Okay? So you'll get used to that.
It's definitely different from other languages
where you're protecting against sending messages to nil.
But you'll get used to it.
You do have to be careful.
If the return type of a message send is a struct
because you don't get back a struct with all its members set
to zero; you get back an undefined struct.
Okay? What's in there is undefined.
Okay? This struct is being returned to you on the stack.
You might get stack garbage,
you might get zero.struct, you just don't know.
So you can't rely on it.
So here you should never rely on that in your code.
Okay? If you call a method, it returns a struct,
you got to check to make sure you're not sending it to nil.
Okay? All right.
Dynamic binding.
So Objective-C handles the dispatching and calling
of methods differently than languages
that most of you are used to.
It does what's called "dynamic binding."
Okay? And you'll see what dynamic binding is
as we go through this slide.
But the important thing to understand about dynamic binding
and about Objective-C is that there's a really,
really important type in Objective-C called "ID," okay?
We call it "ID."
We don't call it id by the way; ID we call it.
So here in that line of code I would say that's ID my object.
Okay? So I'm declaring a pointer,
my object, which is a type ID.
So ID means pointer -- ID is a pointer,
so we don't use ID star; ID is a pointer --
it means a pointer to an object where I don't know its class.
Okay? Of unknown type or unspecified type, okay?
So it's just a pointer to an object;
I do not know anything about that object.
Really, all pointers in Objective-C are IDs
in that the decision about what code to execute
when you send a message to an object is not determined
until runtime when you send that message.
Okay? When you send a message,
it's essentially calling a function that's looking
up the right code to execute for that particular class
that you're pointing to at the time you send the message, okay?
That's called dynamic or late binding, okay?
And all message sends -- all of them in Objective-C --
are done this way, okay?
Now, what does this mean?
All right?
It means that -- well, is this safe, right?
Because you're worried, I'm sure, "What if I send a message
to an object and it doesn't understand it?"
Okay. Well, nothing stops you from doing that.
And not only that, if you do that, your program will crash.
Okay? It will raise an exception.
Unless you catch the exception, it will crash.
And so you're probably feeling like, "Wow.
These Objective-C programs must be crashing all the time
because you accidentally send a message to an object;
it doesn't understand that message."
But actually, it doesn't happen.
Okay. Why doesn't that happen?
And there's really two reasons.
One is we use static typing a lot.
We don't use ID that much.
We use, like, NSString star.
Now, NSString star is exactly the same as using ID,
except that the compiler, while it's compiling your code,
knows that you at least intend
that that pointer points to a string.
So if you tried to send a nonstring message
to that pointer, it's going to complain.
It's going to, you know, give you warnings.
And so when you compile your Objective-C programs they should
always have no warnings, and then you can be pretty sure
that you're not going to have this problem
where you send a message to an object at runtime
and it doesn't understand it and it crashes.
Okay? So that's the number one way.
And the second way I'm going to talk about is that a lot
of times we will check at runtime.
We'll look at that ID pointer, okay?
And we will check and see if it responds
to something before we send it.
Okay? So you know about static typing,
that's the NSString star S equals
at sign X. This is really safe, really good, nothing bad's going
to happen here because the compiler
at compile time has a pretty good idea what you intend
and it will warn you if not.
However, the compiler is not doing the binding
between what codes executes at this time;
it's just warning you.
It's syntactic sugar.
Okay? It's syntax that it can look at to warn you,
but it's not actually enforcing anything here.
It's perfectly legal to say ID obj equals S,
where S is the line before.
Okay? And that will not even generate a warning.
Because ID obj is a pointer to any kind of object;
S is a pointer to any kind of object.
It's a pointer to a string, according to the previous line.
But since those are both pointers to objects,
that's perfectly legal.
Compiler will not warn you.
This is a little bit of a dangerous line of code, right?
Because you just went from having S --
this nice variable that's typed and the compiler can warn you
if you do the wrong thing -- to having an obj,
which is this untyped pointer
where you can send any message you want
and the compiler's not going to warn you.
Okay? You can also do this NSArray star A equals obj.
Okay. Now you have NSArray variable
that points to a string.
Now, that's extremely likely to cause a problem, okay?
This is also legal because NSArray star A is a pointer
to an object; obj is a pointer to an object,
so it allows this assignment.
Very dangerous.
Obviously wrong.
If these three lines of code are there, that's clearly wrong.
You would not want to have an NSArray pointing to an NSString.
Okay? So ID can be dangerous in this way.
And in fact, in the code
that we've written we already did this kind of silently.
Remember that in playing cards match we put a line in there
that said, "Playing card star other car equals other card's
first object."
Remember that?
Well, the method first object in an NSArray returns an ID.
If you go look it up in the documentation,
look at its return type -- parenthesis ID.
Okay? So we just assigned a playing card to an ID.
So that object that came out of
that array better be a playing card
or we're going to crash at runtime.
And a little later in this lecture I'm going
to show you how we can protect our code to make sure
that we don't crash in this case.
Okay? Does everyone see why this is a little dangerous?
All right.
Questions about that?
Okay. And a reminder: Never use ID star.
ID star makes no sense because ID is a pointer.
So ID star would be a pointer to a pointer.
We don't do that in Objective-C, okay,
especially a pointer to an ID [inaudible].
All right.
So to summarize all this I'm going to show you this example.
I've got two classes: I got a vehicle class,
which has what method move --
you can move the vehicle around --
and I got a ship which is a vehicle, inherits from vehicle,
and it's only got one method, shoot.
So this ship can shoot other ships.
And they can also move because they're vehicles.
Okay? So if I declare a local variable ship, ship S,
which is a ship star, and I allocate init a ship,
I can say "S shoot" or "S move" -- works fine, right?
Because ships implement shoot and they inherit move.
So everybody's totally happy here.
I can also do vehicle star V equals S. That's perfectly legal
because V is a vehicle and S is a vehicle
because it inherits its vehicle-ness from ship.
Okay. This is like deck equals playing card deck alloc init --
perfectly legal object to run in your programming, right?
Okay. But now let's try to say V shoot, okay?
Now, what's going to happen here?
All right.
At compile time you're going to get a warning
because vehicles do not implement shoot;
ships do but not vehicles.
So do you see why this is going to give you a warning?
Okay? What about at runtime?
This code will not crash because we know
that V actually is a ship.
So this will work fine at runtime.
Okay? See that?
So this is where you get a warning
at compile time; runs fine at runtime.
Okay? Now, what about this case where I declare an obj, okay,
and it can be anything we want.
Okay? Alloc init it somehow and you say "obj shoot," okay?
No matter what obj is --
and I intentionally didn't tell you what obj is
because it doesn't matter what obj is.
The compiler is not going to warn you here.
Anytime you send a message to obj,
as long as that message exists somewhere,
the compiler will not warn you.
Okay? At runtime, if obj is not a ship or some other object
that responds to shoot, it will crash.
Question?
>> Is ID special in that way?
>> In what way?
>> Because it seems like it's a similar case
where whatever you've assigned obj is a pointer to an object
but it may or may not have this method.
>> Well, so the question is: Is ID special in that way?
You mean is ID special in that I can send it any message I want
and the compiler won't warn me?
Yes. Because that's kind of the semantics of ID.
ID means a pointer to some object I don't know the
class of.
So there's no way for the compiler to give you a warning.
So it just assumes "Okay, somebody responds to shoot.
So it might be this one, so I'm not going to warn you."
So yeah, ID is special in that way, I guess.
That's why we don't want to use ID unadorned like this.
If I'd say obj some method name
that doesn't exist, then it will warn me.
Okay? So if it can't find that method anywhere,
no object that the compiler's ever heard of implements
that method, then it will at least warn you.
Because then it's likely you had a syntax error.
You mistyped it.
Okay? And so the compiler is going to help you there.
So let's say I had an NSString star hello.
Okay? And I'm going to make it equal to at sign hello.
If I say hello shoot, the compiler's obviously going
to warn me and say, "Strings don't respond to shoot."
And at runtime this would crash because, of course,
strings don't respond to shoot.
Okay, so this is all good.
This is the way we want things.
What if I did this, ship star hello ship equals cast --
okay, everyone knows about casting, right,
all languages have casting pretty much; well,
most languages that you know --
parenthesis ship star hello, okay?
This is perfectly legal.
And the compiler will not warn me, okay?
So I've now taken a string and put it in a ship star pointer.
And the compiler now thinks that that hello ship is a ship.
And so it's only going to complain if you do things to it
that don't look like ship, even though we know we know
that that is a string.
Okay. So casting can be super dangerous,
just like in any language casting can be dangerous.
It can be dangerous here.
But we do do cast sometimes.
And the times we cast are the same kind of times we use ID,
which is protected times.
I'm going talk about that protection on the next slide.
So if we then said hello ship shoot, okay,
that would not generate a compiling error
because hello ship is a ship star; however, it would crash
at runtime because strings don't respond to shoot.
Everybody cool?
Yeah?
>> [Inaudible]
>> Okay, the typecasting really doesn't do anything;
that is just tricking the compiler.
Okay? That's not even executing really any code per se.
It's when you send the shoot, it tries to dispatch that shoot
to that variable, that hello ship variable.
The thing turns out to be a string,
and it crashes right there.
You can also do this: ID hello shoot.
Okay? So hello is the NSString star, and I casted it
to ID and sent it shoot.
And that will suppress all warnings as well
because now I'm sending shoot to an ID.
And I just want to show you here that you can cast it right
in line; you don't have to create another variable.
If you cast it right in line, you fake out the compiler.
Now, we don't want to be faking out the compiler.
Compiler is our friend.
It helps us find bugs, okay?
But this all can be done.
Okay? Mostly I put this up here to see the difference
between the compiler catching something and it crashing
at runtime, okay, and when it does which.
All right.
So when would we ever use this ridiculously dangerous thing
of ID?
Well, someone asked earlier, "Can I have an NSArray
that has different kinds of things in it --
some strings, some numbers, something else?"
Absolutely you can.
And there's plenty of times when you absolutely want that, okay?
So it's great for that.
Now, other language like Java lets you actually tell the
compiler that this array has strings in it
so that the compiler, when you pull objects out,
it knows what type they are and it can kind of help you out.
Objective-C doesn't have that.
So the compiler cannot help you with contents array.
Okay? So it's kind of your responsibility.
I'm going to show you how we deal with that.
The second one is -- remember the NBC talk?
I talked about all this blind structured communication, right,
this blind communication.
Well, to have blind communication you got
to have pointers to objects of types you don't know.
Okay? There's no way that the view can send the target action
thing to the controller without knowing the class
of the controller, unless the view can have some kind
of pointer to a controller that it doesn't know the type of.
Okay? And that's big use of ID.
However, we need to protect ourselves,
and there's two big ways that we protect ourselves against IDs.
One is introspection, which I'm going to talk
about in the next few sli
…
[ Pause ]
>> All right.
Well, welcome to lecture four, then, of CS 193P for fall
of 2013/14 -- academic year 2013/14.
Today I'm going to do pretty much all slides.
I do have one brief time when we'll stop and do a little demo.
And then at the beginning of next lecture,
I'll be doing a demo that pretty much demonstrates the vast
majority of what I talk about in the slides today.
And remember, that's kind of how it's always going to go:
I'll show it to you in slides, we'll talk about it,
then I'll demo it to you.
Because when you see a demo, it all makes a lot more sense
than if someone's just talking
in an abstract conceptually about an API.
And then your homework is usually going
to ask you to do this.
So it's, you know, you get a lot out of paying attention
in the lectures because I'm pretty much going
to give you hints and basically tell you a lot of how
to do the homework -- at least from the iOS side of it.
I'm still going to ask you to do the thinking side of it,
the designing side of it.
So today we're going to start off by talking
about a little bit more miscellaneous Objective-C.
And then I'm going to go into talking about Foundation,
which is this framework that has arrays,
and dictionaries, and all that in it.
And we'll start to transition out of Foundation at the end
of that and start talking about things like fonts and colors,
which are obviously not in Foundation
because Foundation is really not a UI-based thing;
it's for building both your model,
and your controller, and your view.
But it's, you know, a layer below the user interface layer.
So let's dive right in.
Let's talk about creating objects.
A few of these things are pretty obvious I hope, but I just want
to put them on slides so that you have them in writing here.
You know that most of the time when we create objects
in the heap, we do it with this alloc init business, right?
So sometimes the init is just init
like NSMutableArray alloc init,
and sometimes it's more complicated init like we had
with our card matching game --
init with card count using deck -- so that you're used to that.
And you've also seen us create objects
in the heap using class methods like string with format.
Remember, we did NSString, string with format.
And that's a class method, right?
Because it's open square bracket, name of a class,
and then class method.
So it gets kind of two different ways you've seen already
to create objects.
Sometimes for some methods,
there's both an init and a class method.
So string with format is an example that there's alloc init
with format and there's NSString, string with format.
They do exactly the same thing.
Both of them are there for kind of historical reasons.
Back before we had this automatic reference counting --
you know, the strong and weak thing --
you had to be much more explicit about things
when you were going to, you know, reference them,
and when they got dereferenced, and when you cleared them
out of the heap, and all that stuff.
And one of the ways that was done was using these class
methods to create the string in a way
that it would get released in certain ways.
So all of that doesn't matter anymore.
So now we're kind of stuck with having both of these things.
It seems to me that iOS, the designers at Apple --
I don't work at Apple, by the way.
Some people have asked me if I work at Apple;
I don't work at Apple.
So I'm just seeing iOS from the outside just like you are.
It looks to me like iOS people are moving towards alloc init,
okay, and kind of away from the class methods.
Although I still see the class methods coming
up in certain circumstances, too.
And there's probably some reasoning behind it.
I haven't seen it put in writing by Apple as to
when they choose which, but you're certainly safe
in this class until they start marketing some of these things
as deprecated, like, don't use them anymore,
to use either form, okay?
I tend to try to go for the alloc inits most of the time,
unless there isn't an alloc init that does what I want;
then I'll use a class method.
But there's other ways to create objects.
Okay? You can create objects by asking another object
to create an object for you.
So that's like string by appending string.
That's not a class method in string;
that's an instance method.
You send it to another string, you give it a second string,
and it will create a new string for you
since strings are immutable.
NSString is immutable.
It creates a new string that is the string you sent it
to with the one you passed as an argument added on, right?
So there it's creating a new object for you, okay?
And a very interesting instance method
that you can ask an object to do for you is mutable copy, okay?
So mutable copy makes a copy of an object but it's mutable.
So if you had an NSString and you send it mutable copy,
you'll get back an NSMutableString.
Or if you have an NSArray and you send it a mutable copy,
you'll get an NSMutableArray.
So that's another way that that happens
that you're creating objects.
Of course, not all methods
that return an object are creating an object like we saw
in NSArray -- last object, and first object,
and object date index.
Those aren't creating new objects;
they're just giving you pointers to the objects
that are in those array.
Okay? That should be completely obvious to you but just want
to make it clear, you know, these are pointers to objects.
And you're allowed to have multiple pointers.
And this is normal pointers here being passed around.
Okay? So basically, unless a method has the word "copy"
in it, then if it's returning to you an object
that exists somewhere else, it's going to be returning a pointer
to that object, okay, unless it has the word "copy" in it like
"mutable copy" or "copy."
And if the object that you're getting back doesn't exist
somewhere, then it's going to make a new one for you.
All right.
That's it on creating objects.
Pretty straightforward there.
Okay. Nil, I already talked about this,
that we can send messages through a pointer that is nil,
i.e. that pointer does not point to anything.
And no code gets executed.
So if I do this init I equals obj method
which returns an init, no code gets executed to execute method
which returns an init; however,
that message sending square bracket thing will return zero.
Okay? So that's important to know.
And zero is also what nil is.
So if you had a method that returned an object and you sent
that message to nil, then you get back nil.
Okay? And we can use that, actually, to our advantage.
For example, in our thing we did on Monday,
we did draw a random card, right?
Self.deck draw a random card.
And that returned nil if the deck was out of cards.
Okay? If the deck itself had been nil,
that also would return nil.
So we can write one if that deals with the fact
that we can't get another card, whether it's
because the deck is empty or because there is no deck
because the deck is nil.
Either way, the code's going to look exactly the same.
Okay? So you'll get used to that.
It's definitely different from other languages
where you're protecting against sending messages to nil.
But you'll get used to it.
You do have to be careful.
If the return type of a message send is a struct
because you don't get back a struct with all its members set
to zero; you get back an undefined struct.
Okay? What's in there is undefined.
Okay? This struct is being returned to you on the stack.
You might get stack garbage,
you might get zero.struct, you just don't know.
So you can't rely on it.
So here you should never rely on that in your code.
Okay? If you call a method, it returns a struct,
you got to check to make sure you're not sending it to nil.
Okay? All right.
Dynamic binding.
So Objective-C handles the dispatching and calling
of methods differently than languages
that most of you are used to.
It does what's called "dynamic binding."
Okay? And you'll see what dynamic binding is
as we go through this slide.
But the important thing to understand about dynamic binding
and about Objective-C is that there's a really,
really important type in Objective-C called "ID," okay?
We call it "ID."
We don't call it id by the way; ID we call it.
So here in that line of code I would say that's ID my object.
Okay? So I'm declaring a pointer,
my object, which is a type ID.
So ID means pointer -- ID is a pointer,
so we don't use ID star; ID is a pointer --
it means a pointer to an object where I don't know its class.
Okay? Of unknown type or unspecified type, okay?
So it's just a pointer to an object;
I do not know anything about that object.
Really, all pointers in Objective-C are IDs
in that the decision about what code to execute
when you send a message to an object is not determined
until runtime when you send that message.
Okay? When you send a message,
it's essentially calling a function that's looking
up the right code to execute for that particular class
that you're pointing to at the time you send the message, okay?
That's called dynamic or late binding, okay?
And all message sends -- all of them in Objective-C --
are done this way, okay?
Now, what does this mean?
All right?
It means that -- well, is this safe, right?
Because you're worried, I'm sure, "What if I send a message
to an object and it doesn't understand it?"
Okay. Well, nothing stops you from doing that.
And not only that, if you do that, your program will crash.
Okay? It will raise an exception.
Unless you catch the exception, it will crash.
And so you're probably feeling like, "Wow.
These Objective-C programs must be crashing all the time
because you accidentally send a message to an object;
it doesn't understand that message."
But actually, it doesn't happen.
Okay. Why doesn't that happen?
And there's really two reasons.
One is we use static typing a lot.
We don't use ID that much.
We use, like, NSString star.
Now, NSString star is exactly the same as using ID,
except that the compiler, while it's compiling your code,
knows that you at least intend
that that pointer points to a string.
So if you tried to send a nonstring message
to that pointer, it's going to complain.
It's going to, you know, give you warnings.
And so when you compile your Objective-C programs they should
always have no warnings, and then you can be pretty sure
that you're not going to have this problem
where you send a message to an object at runtime
and it doesn't understand it and it crashes.
Okay? So that's the number one way.
And the second way I'm going to talk about is that a lot
of times we will check at runtime.
We'll look at that ID pointer, okay?
And we will check and see if it responds
to something before we send it.
Okay? So you know about static typing,
that's the NSString star S equals
at sign X. This is really safe, really good, nothing bad's going
to happen here because the compiler
at compile time has a pretty good idea what you intend
and it will warn you if not.
However, the compiler is not doing the binding
between what codes executes at this time;
it's just warning you.
It's syntactic sugar.
Okay? It's syntax that it can look at to warn you,
but it's not actually enforcing anything here.
It's perfectly legal to say ID obj equals S,
where S is the line before.
Okay? And that will not even generate a warning.
Because ID obj is a pointer to any kind of object;
S is a pointer to any kind of object.
It's a pointer to a string, according to the previous line.
But since those are both pointers to objects,
that's perfectly legal.
Compiler will not warn you.
This is a little bit of a dangerous line of code, right?
Because you just went from having S --
this nice variable that's typed and the compiler can warn you
if you do the wrong thing -- to having an obj,
which is this untyped pointer
where you can send any message you want
and the compiler's not going to warn you.
Okay? You can also do this NSArray star A equals obj.
Okay. Now you have NSArray variable
that points to a string.
Now, that's extremely likely to cause a problem, okay?
This is also legal because NSArray star A is a pointer
to an object; obj is a pointer to an object,
so it allows this assignment.
Very dangerous.
Obviously wrong.
If these three lines of code are there, that's clearly wrong.
You would not want to have an NSArray pointing to an NSString.
Okay? So ID can be dangerous in this way.
And in fact, in the code
that we've written we already did this kind of silently.
Remember that in playing cards match we put a line in there
that said, "Playing card star other car equals other card's
first object."
Remember that?
Well, the method first object in an NSArray returns an ID.
If you go look it up in the documentation,
look at its return type -- parenthesis ID.
Okay? So we just assigned a playing card to an ID.
So that object that came out of
that array better be a playing card
or we're going to crash at runtime.
And a little later in this lecture I'm going
to show you how we can protect our code to make sure
that we don't crash in this case.
Okay? Does everyone see why this is a little dangerous?
All right.
Questions about that?
Okay. And a reminder: Never use ID star.
ID star makes no sense because ID is a pointer.
So ID star would be a pointer to a pointer.
We don't do that in Objective-C, okay,
especially a pointer to an ID [inaudible].
All right.
So to summarize all this I'm going to show you this example.
I've got two classes: I got a vehicle class,
which has what method move --
you can move the vehicle around --
and I got a ship which is a vehicle, inherits from vehicle,
and it's only got one method, shoot.
So this ship can shoot other ships.
And they can also move because they're vehicles.
Okay? So if I declare a local variable ship, ship S,
which is a ship star, and I allocate init a ship,
I can say "S shoot" or "S move" -- works fine, right?
Because ships implement shoot and they inherit move.
So everybody's totally happy here.
I can also do vehicle star V equals S. That's perfectly legal
because V is a vehicle and S is a vehicle
because it inherits its vehicle-ness from ship.
Okay. This is like deck equals playing card deck alloc init --
perfectly legal object to run in your programming, right?
Okay. But now let's try to say V shoot, okay?
Now, what's going to happen here?
All right.
At compile time you're going to get a warning
because vehicles do not implement shoot;
ships do but not vehicles.
So do you see why this is going to give you a warning?
Okay? What about at runtime?
This code will not crash because we know
that V actually is a ship.
So this will work fine at runtime.
Okay? See that?
So this is where you get a warning
at compile time; runs fine at runtime.
Okay? Now, what about this case where I declare an obj, okay,
and it can be anything we want.
Okay? Alloc init it somehow and you say "obj shoot," okay?
No matter what obj is --
and I intentionally didn't tell you what obj is
because it doesn't matter what obj is.
The compiler is not going to warn you here.
Anytime you send a message to obj,
as long as that message exists somewhere,
the compiler will not warn you.
Okay? At runtime, if obj is not a ship or some other object
that responds to shoot, it will crash.
Question?
>> Is ID special in that way?
>> In what way?
>> Because it seems like it's a similar case
where whatever you've assigned obj is a pointer to an object
but it may or may not have this method.
>> Well, so the question is: Is ID special in that way?
You mean is ID special in that I can send it any message I want
and the compiler won't warn me?
Yes. Because that's kind of the semantics of ID.
ID means a pointer to some object I don't know the
class of.
So there's no way for the compiler to give you a warning.
So it just assumes "Okay, somebody responds to shoot.
So it might be this one, so I'm not going to warn you."
So yeah, ID is special in that way, I guess.
That's why we don't want to use ID unadorned like this.
If I'd say obj some method name
that doesn't exist, then it will warn me.
Okay? So if it can't find that method anywhere,
no object that the compiler's ever heard of implements
that method, then it will at least warn you.
Because then it's likely you had a syntax error.
You mistyped it.
Okay? And so the compiler is going to help you there.
So let's say I had an NSString star hello.
Okay? And I'm going to make it equal to at sign hello.
If I say hello shoot, the compiler's obviously going
to warn me and say, "Strings don't respond to shoot."
And at runtime this would crash because, of course,
strings don't respond to shoot.
Okay, so this is all good.
This is the way we want things.
What if I did this, ship star hello ship equals cast --
okay, everyone knows about casting, right,
all languages have casting pretty much; well,
most languages that you know --
parenthesis ship star hello, okay?
This is perfectly legal.
And the compiler will not warn me, okay?
So I've now taken a string and put it in a ship star pointer.
And the compiler now thinks that that hello ship is a ship.
And so it's only going to complain if you do things to it
that don't look like ship, even though we know we know
that that is a string.
Okay. So casting can be super dangerous,
just like in any language casting can be dangerous.
It can be dangerous here.
But we do do cast sometimes.
And the times we cast are the same kind of times we use ID,
which is protected times.
I'm going talk about that protection on the next slide.
So if we then said hello ship shoot, okay,
that would not generate a compiling error
because hello ship is a ship star; however, it would crash
at runtime because strings don't respond to shoot.
Everybody cool?
Yeah?
>> [Inaudible]
>> Okay, the typecasting really doesn't do anything;
that is just tricking the compiler.
Okay? That's not even executing really any code per se.
It's when you send the shoot, it tries to dispatch that shoot
to that variable, that hello ship variable.
The thing turns out to be a string,
and it crashes right there.
You can also do this: ID hello shoot.
Okay? So hello is the NSString star, and I casted it
to ID and sent it shoot.
And that will suppress all warnings as well
because now I'm sending shoot to an ID.
And I just want to show you here that you can cast it right
in line; you don't have to create another variable.
If you cast it right in line, you fake out the compiler.
Now, we don't want to be faking out the compiler.
Compiler is our friend.
It helps us find bugs, okay?
But this all can be done.
Okay? Mostly I put this up here to see the difference
between the compiler catching something and it crashing
at runtime, okay, and when it does which.
All right.
So when would we ever use this ridiculously dangerous thing
of ID?
Well, someone asked earlier, "Can I have an NSArray
that has different kinds of things in it --
some strings, some numbers, something else?"
Absolutely you can.
And there's plenty of times when you absolutely want that, okay?
So it's great for that.
Now, other language like Java lets you actually tell the
compiler that this array has strings in it
so that the compiler, when you pull objects out,
it knows what type they are and it can kind of help you out.
Objective-C doesn't have that.
So the compiler cannot help you with contents array.
Okay? So it's kind of your responsibility.
I'm going to show you how we deal with that.
The second one is -- remember the NBC talk?
I talked about all this blind structured communication, right,
this blind communication.
Well, to have blind communication you got
to have pointers to objects of types you don't know.
Okay? There's no way that the view can send the target action
thing to the controller without knowing the class
of the controller, unless the view can have some kind
of pointer to a controller that it doesn't know the type of.
Okay? And that's big use of ID.
However, we need to protect ourselves,
and there's two big ways that we protect ourselves against IDs.
One is introspection, which I'm going to talk
about in the next few sli
…