Assuming this is not under GC...

On Fri, Oct 3, 2008 at 2:00 PM,  <[EMAIL PROTECTED]> 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 (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to