ChatGPT解决这个技术问题 Extra ChatGPT

Override setter with arc

@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?

Yes, this looks correct to me. Is it working how you expect or not?

E
Evan

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.


Yes. I realize this. I add comment where I want to add my additional code. Thank you very much for reply.
You know what would be an interesting experiment? Changing the comment color (in SO and other sites and IDEs)... usually it's light gray or something that doesn't stand out. #ff0000 maybe? Would it make a difference? We as programmers are used to ignore comments unless we're specifically trying to deduce how something works, in which case we also ignore them sometimes.
m
mattjgalloway

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]


Thanks for this, I was second-guessing my code (and this answer) but you've put my fears to rest.
Thanks Matt. It didn't feel right being the 43rd voter, because 42 seemed such a fitting vote tally for this answer.
What about if the property is set to copy? For example @property (nonatomic, copy) UIColor *lineColor;. Inside the setter, can I just do _lineColor = input; or do I have to do _lineColor = [input copy];?
@DanielT. you have to do _lineColor = [input copy];, yes.
k
kleopatra

Call

[super setImageURLString:theString];

That's it


the superclass is not likely to have an implementation of setImageURLString:
Even if it did, it would probably do stuff you don't want it to do.