Re: How to Handle Array of Dictionaries when Archiving/Unarchiving?
Re: How to Handle Array of Dictionaries when Archiving/Unarchiving?
- Subject: Re: How to Handle Array of Dictionaries when Archiving/Unarchiving?
- From: Alastair Houghton <email@hidden>
- Date: Thu, 23 Jun 2016 14:00:31 +0100
On 23 Jun 2016, at 12:21, Dave <email@hidden> wrote:
>
> I have an Object that I Archive and Unarchive, e.g it conforms to the NSCopying and NSCoding Protocols. The Object in question contains NSArray and NSDictionary Object and I handle them like this:
>
> initWithCoder:
>
> myDictionary = [theCoder decodeObjectForKey:@"pLookUpDictionary "];
> self.pLookUpDictionary = [[NSMutableDictionary alloc] initWithDictionary:myDictionary copyItems:NO];
You don’t need to do that. If you encode a mutable dictionary, you’ll get a mutable dictionary back. The only time that doesn’t happen is with plists or NSUserDefaults; with NS[Keyed]Archiver, you get what you gave it.
> I now need to add another archivable property as so:
>
> @property (nonatomic,strong) NSMutableDictionary<NSString*,NSArray<LTWClassX*>*>* pLookUpDictionaryArray;
>
> How do I handle this in the above three methods? Specially do I need to walk the Array and do a mutableCopy on each of the Dictionaries in the Array?
No. As long as your LTWClassX supports archiving, you can just tell the NSCoder to encode pLookUpDictionaryArray and it will handle the object graph for you. It will even cope if your properties happen to refer to the same object more than once; it ensures that each object is stored only once, and that the resulting object graph is as it was when you saved it.
Kind regards,
Alastair.
--
http://alastairs-place.net
_______________________________________________
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