Re: NSMutableDictionary or Custom Object when adding properties?
Re: NSMutableDictionary or Custom Object when adding properties?
- Subject: Re: NSMutableDictionary or Custom Object when adding properties?
- From: Trygve Inda <email@hidden>
- Date: Thu, 16 May 2013 23:18:30 -0700
- Thread-topic: NSMutableDictionary or Custom Object when adding properties?
> 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).
Will that work right if I have an NSTableView and one of the columns has a
binding to "myCustomProperty" (which is not defined in the object model)...
Will it get sent:
[someObject setValue:someValue forUndefinedKey:myCustomProperty]
Rather than:
[someObject setMyCustomProperty:someValue];
On disk the data will be stored in a plist and an array of NSDictionaryies
(some of the key will be required and predefind) but some will be user
defined.
When I load the data from disk if I use dictionary obejects, I don't have to
do anything else but if I use custom objects I would have to create them and
send
[someObject setValue:someValue forUndefinedKey:myCustomProperty]
To each object.
Right?
Thoughts on this?
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden