2016-04-28 7 views
55

मैं अपने स्विफ्ट आईओएस ऐप में socket.io लागू कर रहा हूं।स्विफ्ट 3.0 और एनएसएनोटिफिकेशन सेंटर में स्विफ्ट 2.0 में नोटिफिकेशन सेंटर का उपयोग करके डेटा कैसे पास करें?

वर्तमान में कई पैनलों पर मैं सर्वर सुन रहा हूं और आने वाले संदेशों की प्रतीक्षा कर रहा हूं। मैं प्रत्येक पैनल में getChatMessage कार्यप्रणाली को कॉल करके इसलिए कर रहा हूँ:

func getChatMessage(){ 
    SocketIOManager.sharedInstance.getChatMessage { (messageInfo) -> Void in 
     dispatch_async(dispatch_get_main_queue(), {() -> Void in 
      //do sth depending on which panel user is 
     }) 
    } 
} 

हालांकि मैंने देखा कि यह एक गलत तरीका है और मैं इसे बदलने की जरूरत है - अब मैं केवल एक बार भेजे गए संदेशों के लिए सुन शुरू करना चाहते हैं और किसी भी संदेश आता है - इस संदेश को किसी भी पैनल को पास करता है जो इसे सुनता है।

इसलिए मैं आने वाले संदेश को NSNotificationCenter के माध्यम से पास करना चाहता हूं। अब तक मैं कुछ जानकारी प्राप्त करने में सक्षम था, लेकिन डेटा को पास नहीं किया। मैं क्या कर रहा था कि द्वारा:

func showSpinningWheel(notification: NSNotification) { 
} 

और किसी भी समय मैं यह कॉल करने के लिए मैं क्या कर रहा था चाहता था:

NSNotificationCenter.defaultCenter().postNotificationName(hideSpinner, object: self) 

तो मैं कैसे कर सकते

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.showSpinningWheel(_:)), name: showSpinner, object: nil) 

तो मैं एक समारोह में कहा जाता था ऑब्जेक्ट messageInfo पास करें और इसे उस फ़ंक्शन में शामिल करें जिसे कॉल किया जाता है?

+1

उपयोग विधि ... 'NSNotificationCenter.defaultCenter() postNotificationName (" hideSpinner ", वस्तु: शून्य, userInfo: yourvalue)।' –

+0

एचएम ठीक है, और कैसे मैं इस 'yourValue' समारोह में लाने के कर सकते हैं कि उस अधिसूचना पर कॉल किया जाता है ('showSpinningWheel' में)? – user3766930

+0

'.userinfo' का उपयोग 'अधिसूचना.userinfo' –

उत्तर

149

स्विफ्ट 2,0

पास जानकारी userInfo जो प्रकार का एक वैकल्पिक शब्दकोश है का उपयोग करते हुए [NSObject: AnyObject]?

let imageDataDict:[String: UIImage] = ["image": image] 

    // Post a notification 
    NSNotificationCenter.defaultCenter().postNotificationName(notificationName, object: nil, userInfo: imageDataDict) 

// Register to receive notification in your class 
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.showSpinningWheel(_:)), name: notificationName, object: nil) 

// handle notification 
func showSpinningWheel(notification: NSNotification) { 
    if let image = notification.userInfo?["image"] as? UIImage { 
    // do something with your image 
    } 
} 

स्विफ्ट 3.0 संस्करण

userInfo अब लेता है [AnyHashable: किसी भी]? एक तर्क है, जो हम स्विफ्ट में शाब्दिक एक शब्दकोश के रूप में प्रदान करते हैं

let imageDataDict:[String: UIImage] = ["image": image] 

    // post a notification 
    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "notificationName"), object: nil, userInfo: imageDataDict) 
    // `default` is now a property, not a method call 

// Register to receive notification in your class 
NotificationCenter.default.addObserver(self, selector: #selector(self.showSpinningWheel(_:)), name: NSNotification.Name(rawValue: "notificationName"), object: nil) 

// handle notification 
func showSpinningWheel(_ notification: NSNotification) { 

    if let image = notification.userInfo?["image"] as? UIImage { 
    // do something with your image 
    } 
} 

नोट के रूप में: अधिसूचना "नाम" अब तार हैं, लेकिन इसलिए हम क्यों NSNotification.Name(rawValue:"notificationName") उपयोग कर रहे हैं प्रकार Notification.Name के हैं, और हम कर सकते हैं अधिसूचना का विस्तार करें। हमारी खुद की कस्टम अधिसूचनाओं के साथ।

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

// and post notification like this 
NotificationCenter.default.post(name: .myNotification, object: nil) 
+0

गोश यह बहुत जटिल है – MarksCode

+6

@ मार्क्सकोड, यह नहीं है, मेरा विश्वास करो :) जैसे ही आप इसे लागू करते हैं, आप देखेंगे कि यह कोड कितना आसान है और हंस जाएगा :)) – mimic

+0

कैसे यह काम करता हैं? उलझन में। अनसुलझा पहचानकर्ता। – HamasN

6

हैलो @sahil मैं तेजी से 3

let imageDataDict:[String: UIImage] = ["image": image] 

    // post a notification 
    NotificationCenter.default.post(name: NSNotification.Name(rawValue: "notificationName"), object: nil, userInfo: imageDataDict) 
    // `default` is now a property, not a method call 

// Register to receive notification in your class 
NotificationCenter.default.addObserver(self, selector: #selector(self.showSpinningWheel(_:)), name: NSNotification.Name(rawValue: "notificationName"), object: nil) 

// handle notification 
func showSpinningWheel(_ notification: NSNotification) { 
     print(notification.userInfo ?? "") 
     if let dict = notification.userInfo as NSDictionary? { 
      if let id = dict["image"] as? UIImage{ 
       // do something with your image 
      } 
     } 
} 

के लिए अपने जवाब को अद्यतन आशा है कि यह उपयोगी है। धन्यवाद UserInfo साथ

+0

मैंने पहले ही अक्टूबर 2016 में ऐसा किया था। – Sahil

+3

अधिसूचना.userinfo होना चाहिए, अधिसूचना नहीं। –

संबंधित मुद्दे