假設(shè)對象A具有一個屬性:@property (nonatomic, strong) Foo * bar;在實現(xiàn)中綜合為:@synthesize bar = _bar;對象B操作a Foo **,如本例中從對象A進行的調(diào)用:Foo * temp = self.bar;[objB doSomething:&temp];self.bar = temp;是否可以合法地進行此操作?該doSomething:方法的正確聲明是什么?此外,假設(shè)在我有機會設(shè)置屬性之前,對象B可能已被釋放bar(從而獲得所指向的實例的所有權(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)我的理解。它是否正確?對象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對象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)生一個編譯時錯誤: passing address of non-local object to __autoreleasing parameter for write-back
3 回答

ibeautiful
TA貢獻1993條經(jīng)驗 獲得超6個贊
您發(fā)現(xiàn)編輯錯誤。文檔中將“輸出”參數(shù)的處理方式稱為“最不壞的解決方案”,因此有些混淆是可以理解的!同樣__autoreleasing
在一個參數(shù)上被證明是推斷出的,因此@Kevin的答案是正確的,但從理論上講是多余的...如果沒有人擊敗我,我稍后會添加一個正確的答案。
- 3 回答
- 0 關(guān)注
- 630 瀏覽
添加回答
舉報
0/150
提交
取消