On 09/08/2012, at 9:47 AM, Quincey Morris <quinceymor...@rivergatesoftware.com> 
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 (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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to