views:

35

answers:

1

My class contains an UIImage property which I want to enforce as a 'copy' property by any external clients accessing it. But, when I try to do a copy in my custom setter, I get the runtime error about UIImage not supporting copyWithZone. So what's a good way to ensure that the correct ownership policy is followed?

// declared in the interface as:
@property (nonatomic, readonly, copy) UIImage *personImage;

// class implementation
- (void)setPersonImage:(UIImage *)newImage
{
    if (newImage != personImage) 
    {
        [personImage release];

        // UIImage doesn't support copyWithZone
        personImage = [newImage copy];
    }
}
+1  A: 

Why do you need copy semantics? The UIImage is immutable, so there's no benefit to setting a copy policy. You just need a copy policy if there's the risk that someone else could modify the image on you. Since the UIImage is immutable, there's no risk of that happening, so a retain property is fine.

If you explain a bit more what you're trying to do, it might be that there's some other way of achieving it.

Jacques