假設(shè)對(duì)象A具有一個(gè)屬性:@property (nonatomic, strong) Foo * bar;在實(shí)現(xiàn)中綜合為:@synthesize bar = _bar;對(duì)象B操作a Foo **,如本例中從對(duì)象A進(jìn)行的調(diào)用:Foo * temp = self.bar;[objB doSomething:&temp];self.bar = temp;是否可以合法地進(jìn)行此操作?該doSomething:方法的正確聲明是什么?此外,假設(shè)在我有機(jī)會(huì)設(shè)置屬性之前,對(duì)象B可能已被釋放bar(從而獲得所指向的實(shí)例的所有權(quán)temp)-我將如何告訴ARC移交擁有的引用?換句話說,如果我希望以下示例代碼起作用,我將如何處理ARC問題?Foo * temp = self.bar; // Give it a reference to some current value[objB doSomething:&temp]; // Let it modify the referenceself.bar = nil; // Basically release whatever we have_bar = temp; // Since we're getting back an owning reference, bypass setter我在想什么呢編輯基于@KevinBallard的回答,我只想確認(rèn)我的理解。它是否正確?對(duì)象A:@implementation ObjectA@synthesize bar = _bar;- (void)someMethod{ ObjectB * objB = [[ObjectB alloc] initWithFoo:&_bar]; // objB handed off somewhere and eventually it's "doSomething" method is called.}@end對(duì)象B:@implementation ObjectB{ Foo * __autoreleasing * _temp;}- (id)initWithFoo:(Foo * __autoreleasing *)temp{ id self = [super init]; if (self) { _temp = temp; } return self;}- (void)doSomething{ ... *_temp = [[Foo alloc] init]; ...}@end這將產(chǎn)生一個(gè)編譯時(shí)錯(cuò)誤: passing address of non-local object to __autoreleasing parameter for write-back
3 回答
ibeautiful
TA貢獻(xiàn)1993條經(jīng)驗(yàn) 獲得超6個(gè)贊
您發(fā)現(xiàn)編輯錯(cuò)誤。文檔中將“輸出”參數(shù)的處理方式稱為“最不壞的解決方案”,因此有些混淆是可以理解的!同樣__autoreleasing在一個(gè)參數(shù)上被證明是推斷出的,因此@Kevin的答案是正確的,但從理論上講是多余的...如果沒有人擊敗我,我稍后會(huì)添加一個(gè)正確的答案。
- 3 回答
- 0 關(guān)注
- 647 瀏覽
添加回答
舉報(bào)
0/150
提交
取消
