• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSMutableDictionary or Custom Object when adding properties?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSMutableDictionary or Custom Object when adding properties?


  • Subject: Re: NSMutableDictionary or Custom Object when adding properties?
  • From: Ken Thomases <email@hidden>
  • Date: Fri, 17 May 2013 01:35:58 -0500

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


_______________________________________________

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

  • Follow-Ups:
    • Re: NSMutableDictionary or Custom Object when adding properties?
      • From: Trygve Inda <email@hidden>
References: 
 >Re: NSMutableDictionary or Custom Object when adding properties? (From: Trygve Inda <email@hidden>)

  • Prev by Date: Re: NSMutableDictionary or Custom Object when adding properties?
  • Next by Date: Re: NSMutableDictionary or Custom Object when adding properties?
  • Previous by thread: Re: NSMutableDictionary or Custom Object when adding properties?
  • Next by thread: Re: NSMutableDictionary or Custom Object when adding properties?
  • Index(es):
    • Date
    • Thread