【问题标题】:Xcode 6: Why this code doesn't compile now?Xcode 6:为什么这段代码现在不能编译?
【发布时间】:2014-09-21 14:42:16
【问题描述】:

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

定义显示警告:自动属性合成不会合成属性“哈希”,因为它是“读写”,但会通过另一个属性“只读”合成

@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;
}

【问题讨论】:

    标签: ios xcode properties


    【解决方案1】:

    【讨论】:

    • 换句话说,将您的属性命名为“hash”以外的任何名称。
    【解决方案2】:

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

    @synthesize hash = _hash;
    

    如果你不想要一个 setter 方法而只想要一个只读属性:

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

    【讨论】:

    • 好的。我们现在必须使用@synthesize hash = _hash; 显式声明自动合成
    • 看看这篇 2012 年关于 Xcode 4.4 的文章中的When To Supply The Instance Variable 部分
    猜你喜欢
    • 1970-01-01
    • 2012-07-06
    • 2010-10-24
    • 1970-01-01
    • 1970-01-01
    • 2011-11-27
    • 2013-08-08
    • 1970-01-01
    相关资源
    最近更新 更多