【发布时间】:2011-09-03 00:37:32
【问题描述】:
我以前避免在变量名中使用下划线,这可能是我大学 Java 时代的遗留问题。因此,当我在 Objective C 中定义一个属性时,我自然会这样做。
// In the header
@interface Whatever
{
NSString *myStringProperty
}
@property (nonatomic, copy) NSString *myStringProperty;
// In the implementation
@synthesize myStringProperty;
但几乎在每个示例中都这样做
// In the header
@interface Whatever
{
NSString *_myStringProperty
}
@property (nonatomic, copy) NSString *myStringProperty;
// In the implementation
@synthesize myStringProperty = _myStringProperty;
我是否应该克服对下划线的厌恶,因为这是它应该做的一种方式,这种风格是否有充分的理由成为首选?
更新:现在使用自动属性合成,您可以省略@synthesize,结果与您使用的一样
@synthesize myStringProperty = _myStringProperty;
这清楚地显示了您的 Apple 偏好。从那以后,我学会了停止担心并喜欢下划线。
【问题讨论】:
-
一个指向原始问题的链接,您认为该问题的重复可以为未来的搜索者节省一些时间。
标签: iphone objective-c coding-style