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.

118 CHAPTER 5 Creating classes<br />

The behavior of the application would be identical. The first form is a more condensed<br />

version of the second. Finally, the method returns the value of self, in part to<br />

enable the condensed [[CTRentalProperty alloc] init]–style creation syntax mentioned<br />

previously.<br />

5.5.3 Combining allocation and initialization<br />

Because it’s common to allocate an object and then want to immediately initialize it,<br />

many classes provide convenience methods that combine the two steps into one.<br />

These class methods are typically named after the class that contains them. A good<br />

example is NSString’s stringWithFormat: method, which allocates and initializes a<br />

new string with the contents generated by the specified format string.<br />

To allow users to easily create a new rental property object, add the following class<br />

method to the CTRentalProperty class:<br />

+ (id)rentalPropertyOfType:(PropertyType)newPropertyType<br />

rentingFor:(float)newRentalPrice<br />

atAddress:(NSString *)newAddress;<br />

This method draws close parallels to the initialization method (listing 5.5) you just<br />

added. The main difference, apart from a slightly different name, is that rental-<br />

PropertyOfType:rentingFor:atAddress: is a class method, whereas initWithAddress:<br />

andPrice:andType: is an instance method. Being a class method, rentalPropertyOf-<br />

Type:rentingFor:atAddress: allows you to invoke the method without first creating an<br />

object. Implement the method as shown in the following listing.<br />

Listing 5.6<br />

Merging allocation and initialization into one easy-to-call method<br />

+ (id)rentalPropertyOfType:(PropertyType)newPropertyType<br />

rentingFor:(float)newRentalPrice<br />

atAddress:(NSString *)newAddress<br />

{<br />

id newObject = [[CTRentalProperty alloc]<br />

initWithAddress:newAddress<br />

rentalPrice:newRentalPrice<br />

andType:newPropertyType];<br />

return [newObject autorelease];<br />

}<br />

Notice that the implementation uses the existing alloc and initWithAddress:renting-<br />

For:andType: methods and returns the newly initialized object to the caller. Another difference<br />

is that the object is also sent an autorelease message. This additional step deals<br />

with a memory-related convention, which we cover in depth in chapter 9.<br />

This just about wraps up our coverage of how to create new classes, but we have<br />

one unpleasant task left to cover: how to dispose of, or deallocate, old and unwanted<br />

objects. If you don’t destroy these objects, they’ll eventually consume enough memory<br />

that the iPhone will think your application is the next Chernobyl and shut it down to<br />

avoid impacting other phone functionality!

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

Saved successfully!

Ooh no, something went wrong!