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.

Overriding methods<br />

133<br />

}<br />

"for $%@ per year with expertise in %@",<br />

[super description], classes, salary, areaOfExpertise];<br />

} else {<br />

return [NSString stringWithFormat:<br />

@"%@. I am a female currently teaching %@ "<br />

"for $%@ per year with expertise in %@",<br />

[super description], classes, salary, areaOfExpertise];<br />

}<br />

Once again, you’ve overridden the description method so that when you use it in your<br />

NSLog method, it will be called rather than the NSObject version of it (see the following<br />

listing).<br />

Listing 6.8<br />

Overriding the description method in Student<br />

- (NSString *)description {<br />

if(gender == Male) {<br />

return [NSString stringWithFormat:<br />

@"%@. I am a male currently enrolled in %@ "<br />

"for %@ credits with %@ as my major",<br />

[super description], classes, numberOfCredits, major];<br />

} else {<br />

return [NSString stringWithFormat:<br />

@"%@. I am a female currently enrolled in %@ "<br />

"for %@ credits with %@ as my major",<br />

[super description], classes, numberOfCredits, major];<br />

}<br />

}<br />

In Teacher and Student, you use their superclass’s (Person) description method to<br />

fill in the first part of the string and then use the individual instance variables of<br />

Student and Teacher, respectively. Custom methods you create can be overridden,<br />

and so can almost all of the include libraries’ methods. Another commonly overridden<br />

method is<br />

- (void)dealloc<br />

This is the method used for memory management when objects need to be released.<br />

This method and its implementation are covered in more detail in chapter 9.<br />

All that’s left to do with these classes is test them and the methods you’ve made for<br />

them. If you go into your Application Delegate and enter the following code into the<br />

applicationDidFinishLaunching:withOptions: method, you’ll be able to see all of<br />

your classes and subclasses working together.<br />

Listing 6.9<br />

Test code for your subclasses<br />

- (void)applicationDidFinishLaunching:(UIApplication *)application<br />

withOptions:(NSDictionary*)options {<br />

// Override point for customization after app launch<br />

[self.window makeKeyAndVisible];

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

Saved successfully!

Ooh no, something went wrong!