简体   繁体   中英

swift - NSCopying class

In Swift 2.1, how should I create a class conforming NSCopying protocol?

I tried this:

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'

But I hit the error of ambiguous user of value . What should I do to fix this problem?

Regards

copyWithZone:返回AnyObject ,因此您必须将副本转换为所需的类型:

var target1 = target.copy() as! TargetValue

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM