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: Fri, 17 May 2013 00:15:02 -0700
- Thread-topic: NSMutableDictionary or Custom Object when adding properties?
> On May 17, 2013, at 1:18 AM, Trygve Inda wrote:
>
>>> On May 17, 2013, at 12:43 AM, Trygve Inda wrote:
>>>
>>>> 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];
>
> Bindings use KVC. They will do:
>
> [someObject setValue:someValue forKey:@"myCustomProperty"];
>
> The KVC machinery is what will eventually call [someObject setValue:someValue
> forUndefinedKey:@"myCustomProperty"] for the dynamic properties. (For the
> predefined properties, it will invoke your setter methods.)
>
>
>> 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?
>
> You can use -setValuesForKeysWithDictionary: to set all of the properties of
> your object from a dictionary. That uses KVC on each key-value pair, so it
> works like I described above for bindings.
>
> That said, you should consider doing proper keyed archiving of your class.
> Don't forget the limits of property lists.
>
> Regards,
> Ken
>
>
So do my user-added properties (that I will store in a dictionary owned by
my custom-class object) have to begin with a lowercase letter to have the
KVC perform correctly?
E.g myCustomField vs MyCustomField?
Of course with the direct dictionary method it would not matter.
Thanks
_______________________________________________
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