可能重複:
How does an underscore in front of a variable in a cocoa objective-c class work?self.variableName與_variableName與@sysnthesize VARIABLENAME
注:對於鄉親們四處想明白這一點,我找出了我混亂的根源。在.h中,我有:
...
@interface myClass : parentClass {
className *variableName:
}
@property (strong, nonatomic) className *variableName;
...
這導致self.variableName和_variableName是.m中的兩個不同的變量。我需要的是:
...
@interface myClass : parentClass {
className *_variableName:
}
@property (strong, nonatomic) className *variableName;
...
然後,在類的.M,self.variableName和_variableName是等價
在全新的Xcode的4.5+,與ARC,瞄準的iOS 5.0 +項目,使用_variableName
而不是self.variableName
與舊式@synthesize variableName
相比有沒有明顯的優勢(運行效率,速度等)?
我的理解是,Xcode中4.5+將創建一個默認的訪問_variableName
等效於self.variableName
,唯一的理由不使用@synthesize variableName
是避免實例變量和傳入的變量,正確之間的混亂呢?
對我而言,僅僅使用self.variableName
來訪問一個iVar似乎是最直接,最清楚你正在尋找哪個變量。除了打字_
與self.
之外,使用_variableName
還有什麼優勢?
這裏還有另外16:http://stackoverflow.com/q/5582448/ http://stackoverflow.com/q/6049269/ http://stackoverflow.com/q/2371489/ HTTP:/ /stackoverflow.com/q/7174277/ http://stackoverflow.com/q/5659156 http://stackoverflow.com/q/837559/ http://stackoverflow.com/q/6146244/ http: //堆棧溢出。com/q/10651535/ http://stackoverflow.com/q/6124109/ http://stackoverflow.com/q/8145373/ http://stackoverflow.com/q/3521254/ http:// stackoverflow.com/q/6064283/ http://stackoverflow.com/q/9696359/ http://stackoverflow.com/q/5521499/ http://stackoverflow.com/q/5466496/ http: //stackoverflow.com/q/2114587/ –
我的不好。我不會想到搜索拼寫出來的'下劃線'。也就是說,檢查一些潛在的二重奏,如果使用_variableName而不是自我,我沒有看到關鍵值觀察,綁定和線程安全訪問(原子)潛在問題的任何/新手證明。 。變量名。其中一個(4088801)提到了一個潛在的問題,如果在_variableName中引用的對象是共享的並且建議使用self.variableName來代替。 –
如果您發現在整個問題的許多實例中有一個被忽略的特定問題,您當然可以隨時詳細詢問。然而,將這些信息恰好埋入另一份副本中幾乎沒有什麼價值。 –