Re: NSString coding errors
Re: NSString coding errors
- Subject: Re: NSString coding errors
- From: James Bucanek <email@hidden>
- Date: Mon, 15 Jan 2007 17:07:05 -0700
Adam Radestock wrote on Monday, January 15, 2007:
>Hi everyone,
>
>I've just added an NSString object to one of my custom objects, which
>conforms to the NSCoding Protocol, but am getting errors whenever I
>try and encode the NSString object.
Knowning what the error was might help.
>Below are my .h and .m files for the custom class, can anyone point
>out the error in my code? I can't see the wood for the trees...
<clip
>@interface SCRPatch : NSObject
>{
> NSMutableDictionary *properties;
> NSMutableDictionary *qcPerameters;
> NSString *patchTypeString;
>}
<clip>
>- (void)encodeWithCoder:(NSCoder *)coder
>{
> if ( [coder isKindOfClass:[NSKeyedArchiver class]] ) {
Bad form. Don't make assumptions based on the class. This should be
if ([coder allowsKeyedCoding])
<clip>
>
>- (id)initWithCoder:(NSCoder *)coder
>{
> properties = [[coder decodeObjectForKey:@"SCRPatchProperties"]
>retain];
> qcPerameters = [[coder decodeObjectForKey:@"SCRPatchPerameters"]
>retain];
> patchTypeString = [[coder decodeObjectForKey:@"patchType"] retain];
> return self;
>}
Whoops! initWithCoder: is an init method, like any other init... method. You aren't calling the base class initializer. The resulting object will be invalid.
(typed in MailSmith)
- (id)initWithCoder:(NSCoder*)coder
{
if ( (self=[super init]) != nil )
{
if ([coder allowsKeyedCoding])
{
properties = [[coder decod...
...
}
else
{
// handle non-keyed coding case
}
}
return (self);
}
--
James Bucanek
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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