01.02.2014 Views

Objective-C Fundamentals

Objective-C Fundamentals

Objective-C Fundamentals

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

58 CHAPTER 3 An introduction to objects<br />

The core concept of inheritance is that a subclass becomes a more specialized version<br />

of its superclass. Rather than describing all of the logic contained in the subclass from<br />

scratch, only the differences in behavior from the superclass need to be specified.<br />

Polymorphism is a related concept that enables you to treat any object, no matter<br />

its class, as if it were typed as one of its superclasses. This works because subclasses can<br />

only extend or modify the behavior of a class; they can’t remove functionality.<br />

3.2 The missing data type: id<br />

In chapter 2 we made one glaringly large omission when discussing the data types<br />

available to represent data in an application. We didn’t cover how to store an object—<br />

something rather important for a language starting with the word <strong>Objective</strong>!<br />

In <strong>Objective</strong>-C a variable, which can represent an object, makes use of a data type<br />

called id. For example:<br />

id name = @"Christopher Fairbairn";<br />

id is a special kind of data type in that it can store a reference to an object of any type.<br />

<strong>Objective</strong>-C also enables you to be more specific about the type of object you expect to<br />

store in a variable. For example, you would more commonly see the previous variable<br />

declaration declared as follows:<br />

NSString *name = @"Christopher Fairbairn";<br />

Here the id data type is replaced by NSString *. Being more explicit about the type of<br />

object you expect to store in the variable enables a number of helpful compile-time<br />

checks. For starters, the compiler will produce an error if you attempt to assign or<br />

store an object of another type to the variable, and a warning will be produced if you<br />

attempt to send the object a message that, to the best of the compiler’s knowledge, it<br />

can’t handle.<br />

There’s no magic with id<br />

You may think there’s some kind of magic occurring with the id data type. How can a<br />

variable of type id store an object of any data type, and why don’t you need to specify<br />

a * character after the data type, as you do with other class names such as NSString?<br />

To answer these questions, declare a variable of type id and then double-click it while<br />

holding down the Cmd key. You should see something similar to the following:<br />

typedef struct objc_object { Class isa; } * id;<br />

This is the declaration of the id data type. It’s another name (a type definition) for a<br />

pointer to a struct called objc_object. The objc_object struct can be considered<br />

as low-level “plumbing” in the <strong>Objective</strong>-C runtime. With a slight amount of hand waving,<br />

you can consider it to be the same as NSObject. Because all objects in <strong>Objective</strong>-C<br />

ultimately derive from NSObject, a variable of type id can store a pointer to<br />

any object, no matter its type. Because the declaration of the id data type contains<br />

a *, one isn’t required when it’s utilized in an application.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!