我有一个数组
:
let myArray = [String]()
我想添加一个 didSet { }
,它知道实际更改的数组索引。
你可以试试这个-
let myArray = [String]()
class YourClassName
{
var array = [1,2,3,4,5]
{
didSet
{
let changedIndexes = zip(array, myArray).map{$0 != $1}.enumerated().filter{$1}.map{$0.0}
print("Changed indexes: \(changedIndexes)")
}
}
}
let demo = YourClassName()
demo.array = [1,2,7,7,5]
// prints: Changed indexes: [2, 3]
关于ios - 数组的属性观察器,获取更改的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49587294/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |