Post

Replies

Boosts

Views

Activity

objectWillChange.sink why not working in swift
I am a Swift beginner; This code in my AppDelegate, it's working! func applicationDidFinishLaunching(_ notification: Notification) {     var join = Contact.instance     var cancellable = Contact.instance.objectWillChange.sink { val in       print("val: \(val)")       print("\(join.age) will change")     }     print(join.haveBirthday())   } // val: () // 24 will change // 25 But, Call havBirthday() from inside ContentView, it's not working. There is no output from the console. struct ContentView: View {       var timer = Timer.publish(every: 1, tolerance: nil, on: .current, in: .common, options: nil).autoconnect();       var body: some View {     VStack{       Text("The name is: \(Contact.instance.name), and age is \(Contact.instance.age)")         .padding()       Button("age+1"){         let age = Contact.instance.haveBirthday()         print("changedAge: \(age)")       }     }   } } class Contact: ObservableObject{   static let instance = Contact(name:"John Appleseed",age:24)       @Published var name: String;   @Published var age: Int;       init(name:String, age:Int){     self.name = name;     self.age = age;   }       func haveBirthday()->Int{     age+=1;     return age;   } }
1
0
1.9k
May ’22