• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSString coding errors
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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

References: 
 >NSString coding errors (From: Adam Radestock <email@hidden>)

  • Prev by Date: Re: NSSlider and NSSliderCell
  • Next by Date: remote proxy object somehow not correct
  • Previous by thread: NSString coding errors
  • Next by thread: Re: NSString coding errors
  • Index(es):
    • Date
    • Thread