Re: NSFileWrapper NSKeyedArchiver and Mutability
Re: NSFileWrapper NSKeyedArchiver and Mutability
- Subject: Re: NSFileWrapper NSKeyedArchiver and Mutability
- From: Graham Cox <email@hidden>
- Date: Tue, 25 Jun 2013 15:51:15 +1000
On 25/06/2013, at 8:53 AM, Trygve Inda <email@hidden> wrote:
> NSMutableDictionary* myMutDict = [[NSMutableDictionary alloc] init];
> NSData* myMutData = [NSKeyedArchiver archivedDataWithRootObject:myMutDict];
>
> If I unarchive this, I was under the impression that it would return an
> immutable object.
Archiving doesn't change the mutability of container objects. Perhaps you were thinking of serializations, which *do* return immutable versions of mutable containers?
> However, if I do:
>
> NSFileWrapper* myWrapper = [[[NSFileWrapper alloc]
> initRegularFileWithContents:myMutData ] autorelease];
>
> [myWrapper setPreferredFilename:myDataName];
>
> [[fileWrappers objectForKey:someKey] addFileWrapper: myWrapper];
>
> And then later read it back in:
>
> NSData* myData = [aWrapper regularFileContents];
>
> NSMutableDictionary* aMutDict = [NSKeyedUnarchiver unarchiveObjectWithData:
> myData ];
>
> I end up with a mutable dictionary.
>
> So...
>
> Will NSKeyedArchiver / NSKeyedUnarchiver preserve mutability?
Yes.
> Will NSFileWrapper initRegularFileWithContents / regularFileContents
> preserve mutability?
No. By this I assume you mean if you pass NSMutableData to -initRegularFileWithContents:, you want NSMutableData from -regularFileContents. No, that returns an NSData*, and in addition I believe it opens the file read only, so the actual content may be simply a piece of read-only mapped memory.
The fact that that same immutable data might dearchive to a mutable container doesn't have any bearing on the NSData, the dearchived objects are independent of it (the archive data is only a description of how to build the object, not the object itself, even though we're encouraged to think of it that way).
--Graham
_______________________________________________
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