{"id":14739,"date":"2016-06-06T15:34:06","date_gmt":"2016-06-06T19:34:06","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=14739"},"modified":"2016-06-06T15:34:48","modified_gmt":"2016-06-06T19:34:48","slug":"avoiding-the-overuse-of-objc-in-swift","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2016\/06\/06\/avoiding-the-overuse-of-objc-in-swift\/","title":{"rendered":"Avoiding the Overuse of @objc in Swift"},"content":{"rendered":"<p>Swift 2.2&rsquo;s <code>#selector<\/code> syntax introduces a new compiler error:<\/p>\n<blockquote cite=\"http:\/\/www.jessesquires.com\/avoiding-objc-in-swift\/\">\n<pre>Argument of '#selector' refers to a method that is not exposed to Objective-C.\n\nFix-it   Add '@objc' to expose this method to Objective-C<\/pre>\n<\/blockquote>\n<p><a href=\"http:\/\/www.jessesquires.com\/avoiding-objc-in-swift\/\">Jesse Squires<\/a>:<\/p>\n<blockquote cite=\"http:\/\/www.jessesquires.com\/avoiding-objc-in-swift\/\"><p>We cannot simply add <code>@objc<\/code> to this method in the original <code>ViewControllerType<\/code> protocol for a number of reasons. If we do, then the entire protocol needs to be marked <code>@objc<\/code>, which means[&#8230;] Our example here is simple, but imagine a much more complex object graph that makes heavy use of Swift&rsquo;s value types and a hierarchy of three protocols with this one in the middle. Introducing <code>@objc<\/code> as the fix-it suggests would <em>break the entire world<\/em> in our app.<\/p>\n<p>[&#8230;]<\/p>\n<p>We can decompose this protocol by separating out all of the <code>@objc<\/code> code into its own protocol. Then, we can use protocol composition to reunite them.<\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Swift 2.2&rsquo;s #selector syntax introduces a new compiler error: Argument of '#selector' refers to a method that is not exposed to Objective-C. Fix-it Add '@objc' to expose this method to Objective-C Jesse Squires: We cannot simply add @objc to this method in the original ViewControllerType protocol for a number of reasons. If we do, then [&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":[54,71,901],"class_list":["post-14739","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-objective-c","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/14739","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=14739"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/14739\/revisions"}],"predecessor-version":[{"id":14740,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/14739\/revisions\/14740"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=14739"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=14739"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=14739"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}