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.

100 CHAPTER 5 Creating classes<br />

Sometimes subtle differences have the most profound effect<br />

In <strong>Objective</strong>-C code it’s common to see the superclass explicitly declared as NSObject.<br />

If you’re a Java or C# developer, this may seem redundant, because in those<br />

languages omitting the superclass causes the compiler to automatically inherit your<br />

class from a predefined superclass (java.lang.Object or System.Object, respectively).<br />

<strong>Objective</strong>-C is similar to these languages in that it supports only single inheritance<br />

but, unlike those languages, it allows for multiple root classes. Not every class<br />

must ultimately inherit from NSObject.<br />

If you don’t explicitly provide a superclass, <strong>Objective</strong>-C declares your class as a new<br />

root class. In other words, the class will sit beside NSObject in the class hierarchy<br />

rather than below it. While this is possible, it generally isn’t desirable because it<br />

means your new class won’t inherit any of the standard functionality typically associated<br />

with an <strong>Objective</strong>-C object. Helpful methods such as isEqual: and memory<br />

management such as alloc, init, and dealloc are all implemented by NSObject.<br />

the class associates with each object created, while the second section declares any<br />

methods and properties the class wishes to allow clients to access.<br />

Before discussing how objects can provide logic to manipulate or work with the<br />

instance data they store, let’s look at how you store data in a class.<br />

5.2.1 Instance variables (ivars)<br />

One of the first steps in defining a new class is to determine the type of data it needs<br />

to store. For the CTRentalProperty class, you want to store data similar to that which<br />

you previously stored in the RentalProperty structure. At a minimum, you’d like to<br />

store the following data for each rental property:<br />

■<br />

Rental price per week (float)<br />

■ Address of property (NSString *)<br />

■<br />

Property type (enum)<br />

In the context of a class, such fields are called instance variables (or ivars, for short).<br />

Instance variables are specified inside a set of curly braces in a class’s @interface declaration<br />

and appear just like ordinary variable declarations: you provide the data type<br />

and name of each variable and terminate each statement with a semicolon. For example,<br />

you could declare the instance variables of the CTRentalProperty class as follows:<br />

float rentalPrice;<br />

NSString *address;<br />

PropertyType propertyType;<br />

Each time you create a new object of the CTRentalProperty class, a unique set of<br />

these variables will be allocated to store the details of this particular instance. Therefore,<br />

if you create multiple CTRentalProperty instances to represent a group of rental<br />

properties, each will be able to store its own unique rentalPrice, address, and<br />

propertyType values.

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

Saved successfully!

Ooh no, something went wrong!