@interface Article : NSObject 

@property (nonatomic, strong) NSString *imageURLString;

@end


@implementation Class

@synthesize imageURLString = _imageURLString;

- (void)setImageURLString:(NSString *)imageURLString {
    _imageURLString = imageURLString;
    //do something else
}

Did I correctly override the setter when ARC is enabled?


Solution 1:

Yes, this is correct. Also took me a while to trust that this is indeed the right thing to do.

You do realize that in this case, the override is not necessary as you don't do more than the standard generated setter would do? Only if you add more code to setImageURLString: would you need to override the setter.

Solution 2:

Expanding on the answer given by @Pascal I'd just like to add that it's definitely the right thing to do and you can check by seeing what the code compiles down to. I wrote a blog post about how to go about checking, but basically that code compiles down to (ARMv7):

        .align  2
        .code   16
        .thumb_func     "-[Article setImageURLString:]"
"-[Article setImageURLString:]":
        push    {r7, lr}
        movw    r1, :lower16:(_OBJC_IVAR_$_Article._imageURLString-(LPC7_0+4))
        mov     r7, sp
        movt    r1, :upper16:(_OBJC_IVAR_$_Article._imageURLString-(LPC7_0+4))
LPC7_0:
        add     r1, pc
        ldr     r1, [r1]
        add     r0, r1
        mov     r1, r2
        blx     _objc_storeStrong
        pop     {r7, pc}

Note the call to _objc_storeStrong which according to LLVM does this:

id objc_storeStrong(id *object, id value) {
    value = [value retain];
    id oldValue = *object;
    *object = value;
    [oldValue release];
    return value;
}

So, to answer your question, yes that's right. ARC has added in the correct release of the old value and retain of the new value.

[Probably over complicated answer, but thought it was useful to show how you can go about answering this sort of ARC related question for yourself in future]