Xcode 6:为什么这段代码现在不编译?

时间:2014-09-21 14:42:16

标签: ios xcode properties

我的代码正在编译并运行良好,直到我升级到Xcode 6。

定义显示警告:自动属性合成不会合成属性' hash'因为它是'readwrite'但它将被合成' readonly'通过另一个属性

@property (nonatomic, strong)               NSString       *hash;       // (get/compute) hash code of the place (master hash of images)

每当我访问_hash时,实现都会显示错误:使用未声明的标识符' _hash'

-(NSString *)hash {
    if (_hash) return _hash;

    // If place id, take it as the hash code
    NSString *poiID = self.info[@"id"];
    if (poiID) {
        _hash = [NSString stringWithFormat:@"id-%lu",(unsigned long)[self.address hash]];
    }
    else if (CLLocationCoordinate2DIsValid(self.location.coordinate)) {
        NSString *seed = [NSString stringWithFormat:@"%f,%f", self.location.coordinate.latitude, self.location.coordinate.longitude];
        _hash = [NSString stringWithFormat:@"location-%lu",(unsigned long)[seed hash]];
    }
    else if (self.address) {
        NSString *seed = self.address;
        _hash = [NSString stringWithFormat:@"address-%lu",(unsigned long)[seed hash]];
    }
    else {
        _hash = @"POI-unknownIDLocationOrAddress";
    }

    return _hash;
}

2 个答案:

答案 0 :(得分:3)

答案 1 :(得分:0)

您需要添加以下行以自动生成setter方法:

@synthesize hash = _hash;

如果您不想要一个setter方法并且只想要一个只读属性:

@property (nonatomic, strong, readonly) NSString *hash;