Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

Share this Page URL
Help

Chapter 12. Copying > Consequences - Pg. 118

Copying { } @property (readwrite, retain) NSString *word; @property (readwrite, retain) NSString *clue; @property (readwrite, copy) NSMutableDictionary *puzzleSpecificAttributes; @end Because the properties are declared readwrite in the class interface, users of the WordMutableInformation class know its instances are mutable. The compiler will ensure that accessor methods exist to both set and re- turn the properties. The -(void)setWord:(NSString *)aString and -(void)setClue::(NSString *)aS- tring methods retain the respective properties. The -(void)setPuzzleSpecificAttributes: (NSMutable- Dictionary *)aDictionary method copies the specified dictionary with code similar to the following: ­(void)setPuzzleSpecificAttributes:(NSMutableDictionary *)aDictionary { if(puzzleSpecificAttributes != aDictionary) { [puzzleSpecificAttributes release]; puzzleSpecificAttributes = [aDictionary copy]; } } Avoid NSCopyObject() Cocoa provides a C function called NSCopyObject() . Don't use it. The NSCopyObject() function makes an exact byte for byte copy of an object. If the object being copied has instance variables that point to other ob- jects, the pointers are copied, but the retain counts of the objects are not incremented as required for Cocoa's reference counted memory management. Even if you use Objective-C 2.0 and automatic memory garbage collection, NSCopyObject() doesn't respect @property declarations and circumvents the optional __strong and __weak declaration modifiers used with garbage collection. The __strong and __weak declaration mod- ifiers are documented at http://developer.apple.com/documentation/Cocoa/Conceptual/GarbageCollection/Ar- ticles/gcAPI.html. Consequences The ability to copy objects is almost as fundamental as the ability to allocate and initialize new instances. Cocoa relies on framework level conventions to allocate and initialize new instances as explained in Chapter 3. The Copying pattern also relies on mere conventions established by the Cocoa frameworks. The Copying pattern enables the use of value semantics with objects and is used in the implementation of Accessors and the Prototype pattern. However, correct implementation of copying conventions requires some forethought by class designers. Support for Cocoa's reference counted memory management must be considered in the implementation of the Copying pattern. The interdependence of Objective-C 2.0 properties and the NSCopying protocol blurs the lines between frame- work features and compiler level language support. Prior to Objective-C 2.0, it was possible to use every feature of the Objective-C language without using Cocoa frameworks at all. Apple's implementation of Objective-C 2.0 depends on the NSCopying protocol and the Copying pattern as implemented by Cocoa. The source code for the Objective-C 2.0 compiler is available from the Gnu Compiler Collection at http://gcc.gnu.org/. Future versions may implement Objective-C 2.0 properties syntax without dependence on Cocoa-specific protocols. 118