Programing

호로 세터 재정의

crosscheck 2020. 8. 6. 07:56
반응형

호로 세터 재정의


@interface Article : NSObject 

@property (nonatomic, strong) NSString *imageURLString;

@end


@implementation Class

@synthesize imageURLString = _imageURLString;

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

ARC가 활성화되었을 때 세터를 올바르게 무시 했습니까?


예, 맞습니다. 또한 이것이 실제로 옳은 일임을 믿기까지 시간이 걸렸습니다.

이 경우 표준 생성 세터보다 많은 작업을 수행하지 않으므로 재정의가 필요하지 않다는 것을 알고 있습니까? 코드를 더 추가 할 경우에만 setImageURLString:setter를 재정의해야합니다.


@Pascal이 제공 한 답변을 확장하면 분명히 옳은 일이며 코드가 컴파일되는 것을 확인하여 확인할 수 있다고 덧붙이고 싶습니다. 확인하는 방법에 대한 블로그 게시물을 작성 했지만 기본적으로 해당 코드는 (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}

에 전화 참고 _objc_storeStrong하는 LLVM에 따라 이 수행을 :

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]


Call

[super setImageURLString:theString];

That's it

참고URL : https://stackoverflow.com/questions/7930654/override-setter-with-arc

반응형