Swift:Notification Observer Using NSNotificationCenter

From Hawk Wiki
Revision as of 04:08, 23 March 2015 by Hall (Talk | contribs) (Created page with "Listen notification in swift <pre class="brush:swift"> func tweetSent(notification: NSNotification) { let newTweet:Dictionary<String, Tweet> = notification.userInfo as Dic...")

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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