gpt4 book ai didi

swift - NSCopying 类

转载 作者:行者123 更新时间:2023-11-28 12:52:09 24 4
gpt4 key购买 nike

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

我试过这个:

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的错误。我应该怎么做才能解决这个问题?

问候

最佳答案

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

var target1 = target.copy() as! TargetValue

关于swift - NSCopying 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36169646/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com