• 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: NSCopyObject is a disaster (was Re: Another NSOutlineView issue)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSCopyObject is a disaster (was Re: Another NSOutlineView issue)


  • Subject: Re: NSCopyObject is a disaster (was Re: Another NSOutlineView issue)
  • From: Graham Cox <email@hidden>
  • Date: Thu, 09 Aug 2012 10:02:34 +1000

On 09/08/2012, at 9:47 AM, Quincey Morris <email@hidden> wrote:

> It seems to me that you need to think of 'copyWithZone:' as a kind of 'init…' method, and it should therefore *not* use properties to change the instance variables that belong to the subclass, but change them directly. This is actually documented:
>
> 	https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Protocols/NSCopying_Protocol/Reference/Reference.html
>
>> "Your options for implementing this protocol are as follows:
>> […]
>> 	• Implement NSCopying by invoking the superclass’s copyWithZone: when NSCopying behavior is inherited. If the superclass implementation might use the NSCopyObject function, make explicit assignments to pointer instance variables for retained objects."
>
> In other words, after calling 'super copyWithZone:', you should immediately fix (zero or overwrite) or retain your instance variables, if they're strong.
>


Indeed - when I mentioned setting a property on the copy I meant after it had been returned by the -copy method.

Even in the light of the documentation above, you're still at the mercy of the superclass's implementation details.

If it happened to use NSCopyObject, any pointers can be simply overwritten or retained. But suppose it did [[[self class] alloc] init]. In that case, your init method is called which is likely to have assigned ivars correctly, retaining them as necessary. Now your copy method doesn't know if it needs to perform the additional retains or not (assuming the worst it will over-retain them and leak), unless you do something really ugly such as setting a flag in the init method and checking it in the copy method to detect which approach the superclass used. Nasty.

--Graham


_______________________________________________

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


References: 
 >Another NSOutlineView issue (From: Graham Cox <email@hidden>)
 >NSCopyObject is a disaster (was Re: Another NSOutlineView issue) (From: Graham Cox <email@hidden>)
 >Re: NSCopyObject is a disaster (was Re: Another NSOutlineView issue) (From: Quincey Morris <email@hidden>)

  • Prev by Date: Re: NSCopyObject is a disaster (was Re: Another NSOutlineView issue)
  • Next by Date: filtering the values in an NSTableColumn
  • Previous by thread: Re: NSCopyObject is a disaster (was Re: Another NSOutlineView issue)
  • Next by thread: Re: NSCopyObject is a disaster (was Re: Another NSOutlineView issue)
  • Index(es):
    • Date
    • Thread