[英]Observe changes in any property of any member of an array
I have an array which stores objects of a class: 我有一个存储一个类的对象的数组:
class Apple {
var color = "Red"
}
let myApple = Apple()
var apples = [Apple]()
apples.append(myApple)
// Func should be called here
myApple.color = "Blue"
let otherApple = Apple()
// Func should not be called here,
// because otherApple is not a member of apples array
otherApple.color = "Green"
I want to run a function when any property of any member of "apples" array has changed. 我想在“ apples”数组的任何成员的任何属性已更改的情况下运行一个函数。 When calling this function, I need to pass the item of the array whose property is changed as a parameter.
调用此函数时,我需要传递属性更改为参数的数组项。
I thought of using didSet
on color property, but in that case function is called when otherApple's property is changed too. 我曾想过在color属性上使用
didSet
,但是在这种情况下,otherApple的属性也被更改时就会调用该函数。 This is not what I want. 这不是我想要的。 I only want to run the function when a property of a member of the array has changed.
我只想在数组成员的属性已更改时运行该函数。 If it is not a member, the function should not run.
如果它不是成员,则该函数不应运行。
Using didSet
, running the function in any case, and checking membership in the beginning of the function might be an idea but I feel this is not a good way. 使用
didSet
, didSet
运行该函数,并在函数的开头检查成员资格可能是一个主意,但我觉得这不是一个好方法。
How can I properly achieve this with Swift? 如何使用Swift正确实现这一目标?
Edit: Apple's guide for Using Key-Value Observing in Swift 编辑:Apple 在Swift中使用键值观察的指南
You need to add the observer
to all the Apple
objects that you're adding in apples
array
. 您需要将
observer
添加到要在apples
array
添加的所有Apple
对象中。
First of all create a property named observers
of type [NSKeyValueObservation]
at class level, ie 首先在类级别创建一个名为
[NSKeyValueObservation]
类型的observers
的属性,即
var observers = [NSKeyValueObservation]()
Now, create a method that will append new Apple
instances in apples array
and add observer
to it, 现在,创建一个将新的
Apple
实例追加到apples array
并为其添加observer
方法,
func addNewApple(_ apple: Apple) {
observers.append(apple.observe(\.color, options: [.new], changeHandler: { (apple, changes) in
if let newValue = changes.newValue {
print(newValue)
}
}))
apples.append(apple)
}
To observe a property of an object, that must be marked it @objc dynamic
. 要观察对象的属性,必须将其标记为
@objc dynamic
。 So, the Apple
definition goes like, 因此,
Apple
定义就像
class Apple: NSObject {
@objc dynamic var color = "Red"
}
Now, you can use it as described below, 现在,您可以按以下说明使用它,
let myApple = Apple()
self.addNewApple(myApple)
myApple.color = "Blue"
Combining all the bits and pieces, the whole code can be written like, 结合所有零碎的部分,整个代码可以这样写:
class VC: UIViewController { var apples = [Apple]() var observers = [NSKeyValueObservation]() override func viewDidLoad() { super.viewDidLoad() let myApple = Apple() self.addNewApple(myApple) myApple.color = "Blue" let otherApple = Apple() otherApple.color = "Green" } func addNewApple(_ apple: Apple) { observers.append(apple.observe(\\.color, options: [.new], changeHandler: { (apple, changes) in if let newValue = changes.newValue { print(newValue) } })) apples.append(apple) } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.