Am Fr,10.10.2008 um 18:17 schrieb Clark Cox:

On Fri, Oct 10, 2008 at 9:09 AM, Negm-Awad Amin <[EMAIL PROTECTED] > wrote:

Am Fr,03.10.2008 um 21:00 schrieb [EMAIL PROTECTED]:

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).
Yup, you are complelty right. The correct code:

if( [BaseClass [EMAIL PROTECTED]( initIWithCoder: )] ) {
…

Sorry for that!

Cheers







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 (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/negm-awad%40cocoading.de

This email sent to [EMAIL PROTECTED]

Amin Negm-Awad
[EMAIL PROTECTED]




_______________________________________________

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/ clarkcox3%40gmail.com

This email sent to [EMAIL PROTECTED]




--
Clark S. Cox III
[EMAIL PROTECTED]

Amin Negm-Awad
[EMAIL PROTECTED]




_______________________________________________

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