hrmncd
hrmncd

Reputation: 1045

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. 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. 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.

How can I properly achieve this with Swift?

Edit: Apple's guide for Using Key-Value Observing in Swift

Upvotes: 0

Views: 1795

Answers (1)

PGDev
PGDev

Reputation: 24341

You need to add the observer to all the Apple objects that you're adding in apples array.

First of all create a property named observers of type [NSKeyValueObservation] at class level, i.e.

var observers = [NSKeyValueObservation]()

Now, create a method that will append new Apple instances in apples array and add observer to it,

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. So, the Apple definition goes like,

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)
    }
}

Upvotes: 3

Related Questions