On May 17, 2013, at 12:43 AM, Trygve Inda wrote:

> I need to keep a small (few thousand) record database of sorts. Each record
> has some pre-detertermined fields, but the user can add there own fields to
> to a limited extent. …
> 
> This will be simple objects in an NSMutableArray.
> 
> I can use an array of NSMutableDictionaries or perhaps an array of
> class-based custom cocoa objects.

Although dictionaries make for a nice quick and dirty object model, I find that 
they very quickly become more cumbersome than they're worth.  With the amount 
of help that the compiler is providing these days, it's ridiculously easy to 
make a small class that does little more than hold some properties.


> One nice thing about the object route is that I can have some fields
> (properties) be based on a calculation rather than real storage like they'd
> be in an NSMutableDictionary.

Yup, that's the sort of case where the limits of dictionaries show up.


> The trouble comes in the fact that I need to be able to add properties at
> runtime. For the dictionary option, it is easy - just make sure the key
> names don't collide and I can add more keys to each dictionary.
> 
> But for the objects I don't see a nice way to do this
> 
> There is setValue:forUndefinedKey: and then each object could keep a local
> dictionary of these "defined at runtime" keys.

That seems "nice" enough to me.  The trick is that the custom class has to be 
sure to only modify the properties via KVC on itself, not the dictionary, in 
order to maintain KVO compliance.  Another way to put it is that only 
-setValue:forUndefinedKey: should ever mutate the dictionary (and it should 
only be invoked by the KVC machinery itself).

Cheers,
Ken


_______________________________________________

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

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

Reply via email to