Re: NSCoding protocol
Re: NSCoding protocol
- Subject: Re: NSCoding protocol
- From: "Stephen J. Butler" <email@hidden>
- Date: Fri, 3 Oct 2008 17:51:39 -0500
Assuming this is not under GC...
On Fri, Oct 3, 2008 at 2:00 PM, <email@hidden> wrote:
> I want MyClass to conform to the NSCoding protocol. But I'm puzzled about
> how to implement the initWithCoder: method.
>
> Suppose I have this in MyClass.h:
>
> NSString *S1, *S2, *S3;
>
>
> and this in its init function:
>
> S1 = @"a string";
> S2 = [[NSString alloc] init];
> S3 = [NSString string];
You need to retain S3 also.
> So to conform to the protocol, I'd have something like this in MyClass.m
> too:
>
> - (id)initWithCoder:(NSCoder *)decoder {
>
> self = [super init];
> S1 = [decoder decodeObjectForKey:@"S1"];
> S2 = [[decoder decodeObjectForKey:@"S2"] retain];
> S3 = [[decoder decodeObjectForKey:@"S3"] retain];
> return self;
> }
>
> (I'm not posting the encodeWithCoder: method.)
>
> So finally, my question: am I right to retain S2 and S3, and not S1?
You need to retain them all. Think of it this way: did you get the
object via alloc, copy, or mutableCopy? No, so you must retain it.
_______________________________________________
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