2015-05-19 14 views
14

में एक अधिसूचना के साथ एकाधिक मान कैसे एक नंबर और एक अधिसूचना के माध्यम से एक स्ट्रिंग भेजने के लिए ...कैसे तेज

let mynumber=1; 
let mytext="mytext"; 
NSNotificationCenter.defaultCenter().postNotificationName("refresh", object: ?????????????); 

और रिसीवर में मान प्राप्त?

func refreshList(notification: NSNotification){ 
     let receivednumber=?????????? 
     let receivedString=????????? 
    } 

उत्तर

22

: उनमें से एक की तरह वस्तुओं की एक सरणी पारित करने के लिए है।

उदाहरण के लिए:

let mynumber=1; 
let mytext="mytext"; 

let myDict = [ "number": mynumber, "text":mytext] 

NSNotificationCenter.defaultCenter().postNotificationName("refresh", object:myDict); 

func refreshList(notification: NSNotification){ 
    let dict = notification.object as! NSDictionary 
    let receivednumber = dict["number"] 
    let receivedString = dict["mytext"] 
} 
+0

धन्यवाद यह काम करता है, लेकिन मैं मान "वैकल्पिक (mytext)" पाठ के लिए मिलता है। क्या यह होना चाहिए और मुझे वैकल्पिक स्ट्रिंग को हटाना है या मैं कुछ गलत कर रहा हूं। –

+0

@mcflysoft विकल्प के बारे में इस पर एक नज़र डालें: https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html#//apple_ref/doc/uid/TP40014097-CH5 -ID330 – Moritz

11

उपयोग userInfo

NSNotificationCenter.defaultCenter().postNotificationName("refresh", object: nil, userInfo: ["number":yourNumber, 
           "string":yourString] 

और पुनः प्राप्त करने के:

func refreshList(notification: NSNotification){ 
    let userInfo = notification.userInfo as Dictionary 
    let receivednumber = userInfo["number"] 
    let receivedString = userInfo["string"] 
} 

इम पर तेज (untested) मजबूत नहीं है, लेकिन आप विचार मिलता है।

3

वास्तव में ऐसा करने के लिए बहुत सारे तरीके हैं। आप उन्हें एक NSArray या एक NSDictionary या एक कस्टम वस्तु में लपेट कर सकता

let arrayObject : [AnyObject] = [mynumber,mytext] 

NSNotificationCenter.defaultCenter().postNotificationName("refresh", object: arrayObject) 

func refreshList(notification: NSNotification){ 

    let arrayObject = notification.object as! [AnyObject] 

    let receivednumber = arrayObject[0] as! Int 
    let receivedString = arrayObject[1] as! String 
} 
20

Xcode 8.3.1 • स्विफ्ट 3,1

extension Notification.Name { 
    static let refresh = Notification.Name("refresh") 
} 

let myDict: [String: Any] = ["myInt": 1, "myText": "text"] 
NotificationCenter.default.post(name: .refresh, object: myDict) 

NotificationCenter.default.addObserver(self, selector: #selector(refreshList), name: .refresh, object: nil) 

// don't forget vvv add an underscore before the view controller method parameter 
func refreshList(_ notification: Notification) { 
    if let myDict = notification.object as? [String: Any] { 
     if let myInt = myDict["myInt"] as? Int { 
      print(myInt) 
     } 
     if let myText = myDict["myText"] as? String { 
      print(myText) 
     } 
    } 
} 
+1

लिंक के लिए धन्यवाद! – MeV

0

स्विफ्ट 4.0, मैं एकल कुंजी गुजर रहा हूं: मान, आप एकाधिक कुंजी और मान जोड़ सकते हैं।

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

ऑब्जर्वर और विधि परिभाषा जोड़ना

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