how to pass multiple values with a notification in swift

前端 未结 6 1744
广开言路
广开言路 2020-11-27 05:06

How to send a number and a String through a notification ...

let mynumber=1;
let mytext=\"mytext\";
NSNotificationCenter.defaultCenter().postNotificationName         


        
6条回答
  •  攒了一身酷
    2020-11-27 05:17

    Swift 4.0, I am passing single key:value, you can add multiple keys and values.

       NotificationCenter.default.post(name:NSNotification.Name(rawValue: "updateLocation"), object: ["location":"India"])
    

    Adding Observer and Method definition. You also need to remove observer.

    NotificationCenter.default.addObserver(self, selector: #selector(getDataUpdate), name: NSNotification.Name(rawValue: "updateLocation"), object: nil)
    
    @objc func getDataUpdate(notification: Notification) {
            guard let object = notification.object as? [String:Any] else {
                return
            }
            let location = object["location"] as? String
            self.btnCityName.setTitle(location, for: .normal)
    
            print(notification.description)
            print(notification.object ?? "")
            print(notification.userInfo ?? "")
        }
    

提交回复
热议问题