Hi,

This is from Apple Sample Code so I thought something as fundamental as this 
would have been dealt with correctly. This is the copy method inside the 
“ImageAndTextCell” class,

-(id) copyWithZone:(NSZone*) zone
{
ImageAndTextCell *cell = (BJImageAndTextCell*) [super copyWithZone:zone];
cell.pTextCellImage = self.pTextCellImage;

return cell;
}

I’ve changed the property to use the copy attribute instead of retain:

@property (nonatomic,copy)      NSImage*                                        
pTextCellImage;


Is this implementation correct? If not what should it be? Something like this?

- (instancetype)initTextCell:(NSString*) theInitialString
{
self = [super initTextCell:theInitialString];
if (self == nil)
        return nil;

self.pTextCellImage = nil;
[self setFont:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]]];

return self;
}

-(id) copyWithZone:(NSZone*) zone
{
ImageAndTextCell *cell = [[[self class] alloc] initTextCell:[self stringValue]];
cell.pTextCellImage = self.pTextCellImage;

return cell;
}

Cheers
Dave

> On 28 May 2015, at 00:25, Graham Cox <graham....@bigpond.com> wrote:
> 
> 
>> On 28 May 2015, at 12:56 am, Dave <d...@looktowindward.com> wrote:
>> 
>> myCell = (ImageAndTextCell*) [cell copy];                            
>> //*******************************************************************************************************************
> 
> 
> It’s a “well known”* fact that a copy of an NSCell or any subclass thereof 
> basically doesn’t work. You have to override -copyWithZone: and Do It 
> Properly™, which means NOT calling super’s implementation first (which 
> internally uses NSCopyObject()). I suspect that’s the root cause of your 
> issue.
> 
> 
> * I say “well known” in quotes because it’s something that lurks there in the 
> frameworks but is never spelt out in documentation, and almost every Mac OS 
> developer comes up against it at some point and has to relearn this fact for 
> themselves. This is your turn.
> 
> —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