相关疑难解决方法(0)

在Objective C中使用下划线前缀属性名称

我以前在变量名称中避免使用下划线,这可能是我大学Java时代的延续.因此,当我在Objective C中定义一个属性时,这就是我自然而然的事情.

// In the header
@interface Whatever
{
    NSString *myStringProperty
}

@property (nonatomic, copy) NSString *myStringProperty;

// In the implementation
@synthesize myStringProperty;
Run Code Online (Sandbox Code Playgroud)

但在几乎所有的例子中都是如此

// In the header
@interface Whatever
{
    NSString *_myStringProperty
}

@property (nonatomic, copy) NSString *myStringProperty;

// In the implementation
@synthesize myStringProperty = _myStringProperty;
Run Code Online (Sandbox Code Playgroud)

我应该克服对下划线的厌恶,因为这是应该做的一种方式,这种风格是否是一个很好的理由?

更新:现在使用自动属性合成你可以省略@synthesize,结果和你使用的一样

@synthesize myStringProperty = _myStringProperty;
Run Code Online (Sandbox Code Playgroud)

这清楚地表明了Apple的偏好.我已经学会了停止担忧并且喜欢下划线.

iphone coding-style objective-c

55
推荐指数
5
解决办法
2万
查看次数

标签 统计

coding-style ×1

iphone ×1

objective-c ×1