Re: NSArchiver and NSKeyedArchiver
Re: NSArchiver and NSKeyedArchiver
- Subject: Re: NSArchiver and NSKeyedArchiver
- From: "Michael Ash" <email@hidden>
- Date: Mon, 22 Sep 2008 09:00:45 -0400
On Sun, Sep 21, 2008 at 11:21 PM, D.K. Johnston <email@hidden> wrote:
> I wrote a class that conforms to the NSCoding protocol. I used
> encodeObject:forKey and decodeObjectForKey: in the required methods.
>
> By mistake I used NSArchiver to archive one of the class instances. It
> compiled with no trouble; but then, not surprisingly, I got an exception
> when I ran it:
>
> *** Terminating app due to uncaught exception 'NSInvalidArgumentException'
> reason: '*** -encodeObject:forKey: only defined for abstract class. Define
> -[NSArchiver encodeObject:forKey:]!'
>
> The fact that I should have used NSKeyedArchiver instead of NSArchiver isn't
> obvious from the message. (At least it's not obvious to me.) Can anything
> be done to ensure users of the class use NSKeyedArchiver, besides putting a
> comment in the header file?
A comment in the header is a pretty good way to go, and a fairly
standard technique. Understanding the error you saw will get easier
with time. It's telling you that you're sending a message to an object
that doesn't understand it. Some inspection shows that it's a keyed
archiving message, and you're sending it to NSArchiver, so the rest
flows from there. Now I don't doubt that it was non-obvious for you
the first time around, but you'll probably only have that difficulty
once.
However, for extra paranoia, Mike Abdullah's technique of checking
explicitly can let you give yourself a better error message.
Mike
_______________________________________________
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