Home > AI > IOS > Combine >

objectWillChange

API

Example

import Foundation

class Contact: ObservableObject {
    @Published var name: String
    @Published var age: Int
    
    
    init(name: String, age: Int) {
        self.name = name
        self.age = age
    }
    
    func changeAge() -> Int {
        self.age += 1
        return self.age
    }
}



class Test {
    init() {
        let john = Contact(name: "John Appleseed", age: 24)
        let c = john.objectWillChange
            .sink { _ in
                print("\(john.age) will change")
            }
        print(john.changeAge())
    }
}

let test = Test()

Relevant tags:

Leave a Reply