【问题标题】:What does an underscore signify in the name of a member variable in Objective-C? [duplicate]Objective-C 中的成员变量名称中的下划线表示什么? [复制]
【发布时间】:2011-08-05 06:35:03
【问题描述】:

可能重复:
Prefixing property names with an underscore in Objective C

我是一名 C/C++ 开发人员,正在学习 Objective-C。最近我开始学习我在网上找到的教程。代码如下:

@interface MapDemoAnnotation : NSObject <MKAnnotation> {
    CLLocationCoordinate2D _coordinate;
}

- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate;

@end

@implementation MapDemoAnnotation

@synthesize coordinate=_coordinate;

- (id)initWithCoordinate:(CLLocationCoordinate2D)coordinate {
    self = [super init];
    if (self != nil) {
        _coordinate = coordinate;
    }
    return self;
}

@end

谁能解释一下这句话的意思

@synthesize coordinate=_coordinate;

我知道@synthesize 的含义。但无法理解完整的陈述。 _coordinate 是一个成员变量。但是coordinate 是什么?它在哪里声明?

【问题讨论】:

  • _被称为下划线,这个问题之前已经在这里问过很多次了。重复:12345

标签: iphone objective-c


【解决方案1】:

基本上,它对编译器没有任何意义。这只是用于私有实例变量的编码实践。如果您不使用它,您的代码不会损坏。

【讨论】:

    猜你喜欢
    • 2011-10-30
    • 2011-04-08
    • 2011-10-16
    • 2011-03-20
    • 1970-01-01
    • 2012-09-29
    • 2011-09-03
    • 1970-01-01
    相关资源
    最近更新 更多