Re: Secure coding NSArray
Re: Secure coding NSArray
- Subject: Re: Secure coding NSArray
- From: Quincey Morris <email@hidden>
- Date: Mon, 15 Feb 2016 09:44:05 -0800
- Feedback-id: 167118m:167118agrif8a:167118sG1s7SpOqu:SMTPCORP
On Feb 15, 2016, at 03:43 , Dave <email@hidden> wrote:
>
> Do you know if same thing applies to dictionaries as well as arrays?
In the project that got me started on this, I don’t yet have any dictionaries, so I don’t know. But I would assume so.
On Feb 15, 2016, at 04:34 , Michael Starke <email@hidden> wrote:
>
> I am unable to reproduce your exception. Is this something related to swift interoperability? In pure objective-c environments it seems to work fine, that is, securely decode without an exception!
There is some other magic-incantation stuff that’s needed to turn on secure archiving. Here’s the code I was using for archiving:
> NSMutableData* data = [NSMutableData data];
> NSKeyedArchiver* archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData: data];
> archiver.requiresSecureCoding = YES;
>
> [archiver encodeObject: model forKey: @"model"];
> [archiver finishEncoding];
or in Swift:
> let data = NSMutableData ()
> let archiver = NSKeyedArchiver (forWritingWithMutableData: data)
> archiver.requiresSecureCoding = true
>
> archiver.encodeObject (model, forKey: "model")
> archiver.finishEncoding ()
and for unarchiving:
> NSKeyedUnarchiver* unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData: data];
> unarchiver.requiresSecureCoding = YES;
>
> model = [unarchiver decodeTopLevelObjectOfClass: [AllContainers class] forKey: @"model" error: outError];
or in Swift:
> let unarchiver = NSKeyedUnarchiver (forReadingWithData: data)
> unarchiver.requiresSecureCoding = true
>
> do {
> model = try unarchiver.decodeTopLevelObjectOfClass (AllContainers.self, forKey: "model")!
> }
> catch let error as NSError { // Without this re-throw, the error is converted to a plain ErrorType, which discards the userInfo dict where the localized error is stored
> throw error
> }
You have to create an archiver/unarchiver manually, in order to tell it to use secure coding. Otherwise, ‘decodeObjectOfClass:forKey:’ (is documented to) decode without producing any errors. Separately, ‘decodeTopLevelObjectOfClass’ has the magic side effect of causing the unarchiving process to change to support proper error handling. The rest of the ‘decode…’ methods work the same, but you can use ‘failWithError:’ while decoding to generate an error which magically tunnels up to the top level. There’s way too much magic here, IMO.
So, my guess is that you didn’t actually turn secure coding on, and therefore didn’t get any errors. However, the entire process is so obscure, and obscurely documented, that you may have found an alternate route to get it working.
For completeness, I’ll add that every class which supports secure coding and has an initWithCoder: method must also implement the ‘supportsSecureCoding’ class property and return YES/true, overriding the super implementation if it exists. It’s not allowable to inherit the superclass property.
_______________________________________________
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