row*_*man 108 iphone ipad ios5 automatic-ref-counting
@interface Article : NSObject
@property (nonatomic, strong) NSString *imageURLString;
@end
@implementation Class
@synthesize imageURLString = _imageURLString;
- (void)setImageURLString:(NSString *)imageURLString {
_imageURLString = imageURLString;
//do something else
}
Run Code Online (Sandbox Code Playgroud)
启用ARC时,我是否正确覆盖了设置器?
Pas*_*cal 89
是的,这是正确的.我还花了一些时间相信这确实是正确的事情.
你确实意识到在这种情况下,覆盖是没有必要的,因为你没有做超过标准生成的setter会做的事情?只有在添加更多代码时才setImageURLString:需要覆盖setter.
mat*_*way 68
扩展@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}
Run Code Online (Sandbox Code Playgroud)
注意调用_objc_storeStrong其根据LLVM做到这一点:
id objc_storeStrong(id *object, id value) {
value = [value retain];
id oldValue = *object;
*object = value;
[oldValue release];
return value;
}
Run Code Online (Sandbox Code Playgroud)
所以,回答你的问题,是的,这是正确的.ARC已添加旧值的正确版本并保留新值.
[可能过于复杂的答案,但认为展示如何在将来为自己回答这类ARC相关问题是有用的]
| 归档时间: |
|
| 查看次数: |
14719 次 |
| 最近记录: |