2016-04-28 12 views
55

私は速いiosアプリでsocket.ioを実装しています。Swift 3.0でNotificationCentreを使用し、swift 2.0でNSNotificationCenterを使用してデータを渡す方法は?

現在、いくつかのパネルで、私はサーバーを聞いて、着信メッセージを待っています。今、私は一度だけ受信メッセージのリッスンを開始したいと時に任意のメッセージ - しかし、私はそれは間違ったアプローチだ気づき、私はそれを変更する必要が

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

:私は、各パネルのgetChatMessage関数を呼び出すことによってそうしますよこのメッセージを聞くパネルにこのメッセージを渡します。

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

userinfo ... NSNotificationCenter.defaultCenter()。postNotificationName( "hideSpinner"、オブジェクト:nil、userInfo:yourvalue)を使用するメソッド –

+0

hm ok、そしてこの 'yourValue'をどのようにして取得できますか?その通知( 'showSpinningWheel')で呼び出されますか? – user3766930

+0

'.userinfo'を' notification.userinfo'のように使用しています –

答えて

149

タイプのオプション辞書であるuserInfoを用いて2.0

パス情報[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 
    } 
} 

NOTEでリテラル辞書として提供引数として:通知「名前」はもはや文字列ではないが、型Notification.Nameのものであり、それゆえ、なぜ私たちはNSNotification.Name(rawValue:"notificationName")を使用していて、我々はできます独自のカスタム通知でNotification.Nameを拡張します。私は迅速な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 
      } 
     } 
} 

ためのあなたの答えを更新@sahil

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

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

こんにちは。 – MarksCode

+6

@MarksCode、それはありません、私を信じてください:)あなたはそれを実装するとすぐに、このコードがどれくらいシンプルで、笑ってくれるのでしょう:)) – mimic

+0

できます?混乱している。未解決の識別子。 – HamasN

6

こんにちは、それが参考に願っています。ありがとうございました

+0

私はすでに2016年10月にそれをしました。 – Sahil

+3

はnotification.objectではなく、notification.userinfoでなければなりません –

関連する問題