{"id":10568,"date":"2015-01-23T18:30:52","date_gmt":"2015-01-23T23:30:52","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=10568"},"modified":"2015-01-23T18:39:02","modified_gmt":"2015-01-23T23:39:02","slug":"lets-build-swift-notifications","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2015\/01\/23\/lets-build-swift-notifications\/","title":{"rendered":"Let&rsquo;s Build Swift Notifications"},"content":{"rendered":"<p><a href=\"https:\/\/mikeash.com\/pyblog\/friday-qa-2015-01-23-lets-build-swift-notifications.html\">Mike Ash<\/a>:<\/p>\n<blockquote cite=\"https:\/\/mikeash.com\/pyblog\/friday-qa-2015-01-23-lets-build-swift-notifications.html\">\n<p>NSNotifications are both simple and powerful, which is why they show up so often in the frameworks. Specifically, they have a few distinct advantages:<\/p>\n<ol>\n<li>Loose coupling between notification senders and receivers.<\/li>\n<li>Support for multiple receivers for a single notification.<\/li>\n<li>Support for custom data on the notification using the <code>userInfo<\/code> property.<\/li>\n<\/ol>\n<p>There are some disadvantages as well:<\/p>\n<ol>\n<li>Sending and registering for notifications involves interacting with a singleton instance with no clear relationship to your classes.<\/li>\n<li>It&rsquo;s not always clear what notifications are available for a particular class.<\/li>\n<li>For notifications which use <code>userInfo<\/code>, it&rsquo;s not always clear what keys are available in the dictionary.<\/li>\n<li><code>userInfo<\/code> keys are dynamically typed and require cooperation between the sender and receiver that can&rsquo;t be expressed in the language, and messy boxing\/unboxing for non-object types.<\/li>\n<li>Removing a notification registration requires an explicit removal call.<\/li>\n<li>It&rsquo;s difficult to inspect which objects are registered for any given notification, which can make it hard to debug.<\/li>\n<\/ol>\n<p>My goal in reimagining notifications in Swift is to remedy these problems.<\/p>\n<\/blockquote>\n<p>He runs into an interesting case where casting is necessary to satisfy Swift&rsquo;s type system. I didn&rsquo;t expect this, and it&rsquo;s instructive to think about what is going on here. Also note how it transparently handles observer functions with different numbers of parameters.<\/p>","protected":false},"excerpt":{"rendered":"<p>Mike Ash: NSNotifications are both simple and powerful, which is why they show up so often in the frameworks. Specifically, they have a few distinct advantages: Loose coupling between notification senders and receivers. Support for multiple receivers for a single notification. Support for custom data on the notification using the userInfo property. There are some [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"","apple_news_api_id":"","apple_news_api_modified_at":"","apple_news_api_revision":"","apple_news_api_share_url":"","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[69,31,46,30,74,71,901],"class_list":["post-10568","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-cocoa","tag-ios","tag-languagedesign","tag-mac","tag-opensource","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/10568","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=10568"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/10568\/revisions"}],"predecessor-version":[{"id":10569,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/10568\/revisions\/10569"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=10568"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=10568"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=10568"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}