Re: NSCoding protocol
Re: NSCoding protocol
- Subject: Re: NSCoding protocol
- From: "Clark Cox" <email@hidden>
- Date: Fri, 10 Oct 2008 09:17:20 -0700
On Fri, Oct 10, 2008 at 9:09 AM, Negm-Awad Amin <email@hidden> wrote:
>
> Am Fr,03.10.2008 um 21:00 schrieb email@hidden:
>
>> 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];
>>
>> So to conform to the protocol, I'd have something like this in MyClass.m
>> too:
>
> In addition to the replies I want to give a hint to something else:
>
>> - (id)initWithCoder:(NSCoder *)decoder {
>>
>> self = [super init];
>
> This is only correct, if your base class does not support NSCoding.
> Otherwise you have to call -initWithCoder: (super).
>
> Because this depends of the base class, we have a fragile base class
> problem. (Adding NSCoding to the base class looks like doing everything
> correct inside the base class, but will cause MyClass as a subclass to a
> malfunction.) Keep in mind, that NSCoding can be added through a category.
> So it looks safer to do:
>
> if( [super respondsToSelector:@selector( initWithCoder: )] {
> self = [super initWIthCoder:decoder];
> } else {
> self = [super init];
> }
No, this will not work. Unless you've implemented -respondsToSelector:
yourself, [super respondsToSelector:] and [self respondsToSelector:]
will always return the exact same value (because they call the exact
same method).
>
> I never had this problem in practice, but theoretically … (But maybe I got a
> paranoia.)
>
> Cheers
>
>
>
>
>
>
>> 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?
>> _______________________________________________
>>
>> 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
>
> Amin Negm-Awad
> email@hidden
>
>
>
>
> _______________________________________________
>
> 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
>
--
Clark S. Cox III
email@hidden
_______________________________________________
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