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

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