Challenge question: Storing NSData in an object


When I read the challenge, I was wondering how I would get all of the various objects types into an array, which was what seemed logical to me. I thought the best way was to create an instance of an object with all of the instance variables I needed and then put that object into an array of objects. My plan was working, but then I tried to create “@property” accessor methods for all of the variables. That apparently does not work for pointers to other objects referenced in the object:

@interface PList : NSObject { NSMutableDictionary *person; // NSString *name; NSDate *bDate; NSMutableData *pix; . . . . . } @property NSMutableDictionary *person; //This does not work. @property NSMutableData *pix; //This does not work. . . . . . . . @end

How do you associate something like a data object (picture) with a particular instance of an object?


Maybe I found my own answer. I’m running XCode 3, and I found in an older guidebook the suggestion that you have to use a (retain) keyword before the object because of memory management. Maybe this is no longer necessary with ARC? Here’s an example of what a property would look like under XCode 3 (I think):

@property (retain) NSMutableData *pix;

Does this sound right? Is my approach to the challenge all wrong?


You’re headed down a good road here.

@property declarations for object pointers require a storage specifier such as strong, weak, copy, or unsafe_unretained. Pre-ARC, we would use retain instead of strong, and assign instead of weak/unsafe_unretained.

@property declarations for primitive types (int, float, etc.) don’t require a storage specifier to be used explicitly - they’ll assume ‘assign’ if you don’t provide one.

Keep in mind what you learned in Chapters 19 and 20 related to memory management, and have a look at Chapter 30: Properties.