class Model: NSObject {
var numbers : Array<Int> = []
}
internal func test() {
let model : Model = Model.init()
model.setValue([1,2,3], forKey: "numbers")
print(model.numbers)
}
test()//[1, 2, 3]
但如果我将上述var numbers : [Int] = []
更改为var numbers :[Int64] = []
,它就会崩溃。这是为什么?我该如何解决?
答案 0 :(得分:0)
首先,在这种情况下不要使用KVC。使用带点符号的setter。
其次,如果你想要[Int64]
转换类型
class Model: NSObject {
var numbers = [Int64]()
}
internal func test() {
let model = Model()
model.numbers = [1,2,3] as [Int64]
print(model.numbers)
}
test()//[1, 2, 3]