【发布时间】:2012-12-04 04:23:44
【问题描述】:
可能重复:
How does an underscore in front of a variable in a cocoa objective-c class work?
Difference between self.ivar and ivar?
Synthesized property and variable with underscore prefix: what does this mean?
要在对象 c 中使用属性,我有两种选择,我应该使用哪一种?
选择1:self.property = xxxx;
选择2:_property = xxx
例如:
//.h file
@interface ViewController : UIViewController
@property (retain, nonatomic) NSArray *myArray;
@end
//.m file
@interfaceViewController ()
@end
@implementation ViewController
- (void)doing {
_myArray = [[NSArray alloc] init]; //choice one
self.myArray = [[NSArray alloc] init]; //choice two
}
@end
【问题讨论】:
-
@CodaFi 请关注骗子链 :)
-
@Jack,这是他的事。 :P
-
问题不在于下划线语法。他在问两种设置变量的方式之间的区别,这不是语法或约定的问题。
-
不完全是,这个问题是关于属性的声明,而不是关于 ivar 的访问和使用。
-
这是有还是没有
ARC?