• 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: NSCoding protocol
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSCoding protocol


  • Subject: Re: NSCoding protocol
  • From: Negm-Awad Amin <email@hidden>
  • Date: Fri, 10 Oct 2008 18:52:41 +0200


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

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

if( [BaseClass instancesRepondsToSelector@selector( 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 (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:
email@hidden


This email sent to email@hidden




-- Clark S. Cox III 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


  • Follow-Ups:
    • Re: NSCoding protocol
      • From: "Michael Ash" <email@hidden>
References: 
 >NSCoding protocol (From: email@hidden)
 >Re: NSCoding protocol (From: Negm-Awad Amin <email@hidden>)
 >Re: NSCoding protocol (From: "Clark Cox" <email@hidden>)

  • Prev by Date: inconsistent float behaviour
  • Next by Date: Re: CalendarStore sync failure
  • Previous by thread: Re: NSCoding protocol
  • Next by thread: Re: NSCoding protocol
  • Index(es):
    • Date
    • Thread