• 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: cocoa-dev digest, Vol 2 #1058 - 10 msgs
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: cocoa-dev digest, Vol 2 #1058 - 10 msgs


  • Subject: Re: cocoa-dev digest, Vol 2 #1058 - 10 msgs
  • From: Kirk Kerekes <email@hidden>
  • Date: Tue, 6 Aug 2002 12:12:23 -0500

On Tuesday, August 6, 2002, at 11:00 AM, email@hidden wrote:

// Impl
- (id)initWithCoder:(NSCoder *)coder
{
if (self = [super init])
{
// theStroke = [coder decodeObject];
theStroke = [[coder decodeObject] retain];
}
return self;
}

Points:

1. Unless your objects are subclasses of NSObject (or another class that doesn't implement <NSCoding>), you must call [super decodeObject] and [super encodeObject] in your initWithCoder and codeWithCoder methods.

2. Because NSOBject does not itself support encoding, retain counts are not preserved across encode/decode cycles. This is probably a good thing, btw,
and it is entirely consistent with the Cocoa "Rules Of Object Acquisition"
. Assume an object returned from decodeObject has been autoreleased just like it would be from a +convenience routine. If you intend to hold on to it, you have to retain it.

3. Use accessors _everywhere_ (except _in_ the accessors). Don't let the currently bubbling argument here over accessor patterns cause you to defer adopting appropriate getters and setters. I happen to fall into the "simpler is better" camp, so I would recommend any of the recommended simple (no autoreleases or locks) versions. I use the free AccessorBuilder for generating accessors automagically from ivs. I don't particularly like AccessorBuilder's hard-coded accessor pattern, and it doesn't handle all possibilities correctly (it screws up id accessors, of all things, because it doesn't recognize that an id is an object pointer) but it is free, functional and readily available.

I make point 3 because if you were using proper accessors, you never would have had your problem in the first place -- your initWithCoder line would read:

[self setTheStroke:[coder decodeObject]];

-- and setTheStroke would have internally done the retain for you.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Prev by Date: Re: Accessor methods and (auto)release: conclusion
  • Next by Date: Re: Accessor methods and (auto)release: conclusion
  • Previous by thread: Re: Print Dialog in Mac OS X
  • Next by thread: ThreadWorker question
  • Index(es):
    • Date
    • Thread