On 23/10/2009, at 12:27 PM, Michael de Haan wrote:


Let me start by saying I **think** I understand why my first approach did not work, but am interested if my working approach is correct in concept.

Background.
As a **very long** detour from Hillegass's chapter 18 challenge, I am following along with mmalc's excellent series of Binding examples. Thus, now creating a table of 4 columns without bindings. (He uses 1 column, but I added a few).


In order to create the NSMutableDictionary object, which will be added to an NSDoc Array ivar, (which will be used by the datasource methods of a table) I have created a

Record Object with one ivar (  _record ( An NSMutableDictionary).)

which returns:

First approach: ( did **not** work)

-(id) init
{
     self = ....snip...usual code
      NSString * _name = @"John Doe";
      NSString *  _city
      ....snip...other string properties


OK, these are NOT "properties". They are simply local variables. (Also, don't snip code, it makes it really hard to tell whether what you've done is right, as you have to mentally put back what should be there - who knows if it matches what you actually have?)

     _record = [ NSMutableDictionary dictionaryWithObjectsAndKeys:
      _name, MDHNameKey,
      ....snip...
     nil;
   }
 return self;
}


As I learnt, self returns an empty object. ( If I understand it correctly, the local variables _name etc are no longer in scope once self is returned.)

This should not return an empty object, but it should return one having the ivar _record. One question is whether you're using garbage collection or not, because if not, then _record is shortly going to get autoreleased, leading to a stale reference and almost certain disaster (unless you've also snipped out vital memory management calls as well as the closing square bracket).

If you are truly getting an empty object, then you'll have to show all the code you've snipped to be able to say why.

Second approach.

Record Object with 4 ivars viz
_name
_city etc etc


Self now returns an object whose attributes are now, in the calling method ( the "AddRecord") used to create the NSMutableDictionary object and added "model" array, and the table populates as expected.

OK, read this about twenty times now and still have no clear idea what you mean. What mutable dictionary? Having done it this way, you no longer need one as far as I can see.

May I ask if this is the correct approach? ...or should one still try and aim to **return** a dictionary object to the calling method?

Personally I'd say your second approach is more usual. You *could* store your properties in a dictionary - this storage is an implementation detail of your record class. However it's usually easier just to have a separate ivar for each one, which is how the KVC system is typically implemented. If you exposed your 'record' property as a property in itself, you could access individual properties using a keypath such as yourObject.record.someKey, but typically you'd just want yourObject.someKey so that the implementation of the property storage is not exposed.

--Graham




_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to