亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

處理ARC中的指針對指針所有權問題

處理ARC中的指針對指針所有權問題

慕仙森 2019-11-25 10:55:41
假設對象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的答案是正確的,但從理論上講是多余的...如果沒有人擊敗我,我稍后會添加一個正確的答案。

查看完整回答
反對 回復 2019-11-25
  • 3 回答
  • 0 關注
  • 624 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號