

The compiler’s type checking is called static typing, as opposed to the dynamic behavior that takes place when the program actually runs.

Of course, it still makes a difference at runtime whether an id is nil or something else sending a message to nil won’t crash the program, but sending an unknown message to an actual object will. Nil is a form of zero, it's zero cast as an id. Thus, every instance reference is also an id.Any object value can be assigned or typecast to an id, and vice versa. It is defined to mean “an object pointer,” plain and simple, with no further specification. An id is a pointer, so you don’t say id*. Objective-C also provides a special type designed to silence the compiler’s worries about object data types altogether. There are languages that permit this feature, called overloading, but Objective-C is not one of them. It is illegal for two methods of the same type (class method or instance method) to exist in the same class with the same name but different signatures. It’s simply a form of zero - the form of zero appropriate to an instance reference.The nil value simply means: “This instance reference isn’t pointing to any instance.” Sending a message to a garbage pointer, or otherwise treating it as a meaningful instance, can crash your program.įor this reason, if you aren’t going to initialize an instance reference pointer at the moment you declare it by assigning it a real value, it’s a good idea to assign it nil:
#Objective c block boostnote code
You have created a pointer, but you haven’t supplied an NSString for it to point to.It is claiming to be a pointer to an NSString, and so your code might proceed to treat it as a pointer to an NSString.But it is pointing at garbage.Ī pointer pointing at garbage is liable to cause serious trouble down the road when you accidentally try to use it as an instance. NSString* s // only a declaration no instance is pointed toĪfter that declaration, s is typed as a pointer to an NSString, but it is not in fact pointing to an NSString. Merely declaring an instance reference’s type doesn’t bring any instance into existence. Instance References, Initialization, and nil

In general, in Objective-C, a reference to an instance is a pointer and the name of the data type of what’s at the far end of that pointer is the name of the instance’s class. In Objective-C, if a variable is an instance of the class M圜lass, that variable is of type M圜lass* - a pointer to a M圜lass.
