swift - NSCopying类

时间:2016-03-23 04:05:11

标签: swift ambiguous nscopying

在Swift 2.1中,我应该如何创建符合NSCopying协议的类?

我试过了:

class TargetValue: NSObject, NSCopying {

    var value: Int?

    func copyWithZone(zone: NSZone) -> AnyObject {
        let copy = TargetValue()
        copy.value = value
        return copy
    }
}

var target = TargetValue()
target.value = 12

var target1 = target.copy()
print(target1.value ) // ambiguous user of 'value'

但是我遇到了ambiguous user of value的错误。我该怎么做才能解决这个问题?

此致

1 个答案:

答案 0 :(得分:2)

copyWithZone:返回AnyObject,因此您必须将副本转换为预期类型:

var target1 = target.copy() as! TargetValue