Exercises

1. Implement a copy method for the AddressBook class according to the NSCopying protocol. Would it make sense to also implement a mutableCopy method? Why or why not? Also, think about what happens if someone uses the setter method for the book property in the AddressBook class. Who should own the address book that gets passed as the argument to the setter? How can you fix this?

2. Modify the Rectangle and XYPoint classes defined in Chapter 8 to conform to the <NSCopying> protocol. Add a copyWithZone: method to both classes. Make sure that the Rectangle copies its XYPoint member origin using the XYPoint’s copy method. Does it make sense to implement both mutable and immutable copies for these classes? Explain.

3. Create an NSDictionary ...

Get Programming in Objective-C, Sixth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.