Difference between revisions of "Swift:Notification Observer Using NSNotificationCenter"

From Hawk Wiki
Jump to: navigation, search
m
(Listen Notification in Swift)
Line 4: Line 4:
 
     let newTweet:Dictionary<String, Tweet> = notification.userInfo as Dictionary<String, Tweet>
 
     let newTweet:Dictionary<String, Tweet> = notification.userInfo as Dictionary<String, Tweet>
 
}
 
}
NSNotificationCenter.defaultCenter().addObserver(self, selector: "tweetSent:", name: "newTweetNotification", object: nil)
+
NSNotificationCenter.defaultCenter().addObserver(self, selector: "tweetSent", name: "newTweetNotification", object: nil)
 
</pre>
 
</pre>
 +
 
==Send/Post Notification in Swift==
 
==Send/Post Notification in Swift==
 
<pre class="brush:swift">
 
<pre class="brush:swift">

Revision as of 06:33, 23 March 2015

Listen Notification in Swift

func tweetSent(notification: NSNotification) {
    let newTweet:Dictionary<String, Tweet> = notification.userInfo as Dictionary<String, Tweet>
}
NSNotificationCenter.defaultCenter().addObserver(self, selector: "tweetSent", name: "newTweetNotification", object: nil)

Send/Post Notification in Swift

var userInfo: Dictionary<String, Tweet> = ["tweet": newTweet]
NSNotificationCenter.defaultCenter().postNotificationName("newTweetNotification", object: nil, userInfo: userInfo)

Remove Notification Observer

deinit {
    NSNotificationCenter.defaultCenter().removeObserver(self)
}