{"id":18578,"date":"2017-08-09T15:06:10","date_gmt":"2017-08-09T19:06:10","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=18578"},"modified":"2017-08-09T15:06:10","modified_gmt":"2017-08-09T19:06:10","slug":"protocol-composition-in-swift-and-objective-c","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2017\/08\/09\/protocol-composition-in-swift-and-objective-c\/","title":{"rendered":"Protocol Composition in Swift and Objective-C"},"content":{"rendered":"<p><a href=\"http:\/\/www.jessesquires.com\/blog\/protocol-composition-in-swift-and-objc\/\">Jesse Squires<\/a>:<\/p>\n<blockquote cite=\"http:\/\/www.jessesquires.com\/blog\/protocol-composition-in-swift-and-objc\/\"><p>Any protocol in Swift that contains optional members must be marked as <code>@objc<\/code>. I have <a href=\"https:\/\/www.jessesquires.com\/blog\/avoiding-objc-in-swift\/\">written before<\/a> about avoiding <code>@objc<\/code> in your Swift code as much as possible. When <code>@objc<\/code> infiltrates your object graph, nearly everything must inherit from <code>NSObject<\/code> which means you cannot use Swift structs, enums, or other nice features. This leaves you not writing Swift, but merely &ldquo;Objective-C with a new syntax&rdquo;.<\/p><p>[&#8230;]<\/p><p>A better approach is to split up large protocols into smaller ones, and provide a unique property (like a delegate) for each one.<\/p><p>[&#8230;]<\/p><p>This design transfers the &ldquo;optional-ness&rdquo; from the protocol itself to an additional optional property on the class. If you want headers and footers in your table view, you can opt-in to those by setting <code>titlesDataSource<\/code>. To opt-out, you can set this property to <code>nil<\/code>. The same applies to <code>reorderingDataSource<\/code>, and so on.<\/p><p>[&#8230;]<\/p><p>Instead of numerous disjoint protocols, you can design a union of protocols. This provides a single, top-level &ldquo;entry point&rdquo; to reference. You can extract the optional members of a protocol into a new protocol, then add an optional property for this new protocol on the original protocol. The result is a comprehensive top-level protocol and a set of &ldquo;nested&rdquo; protocols.<\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Jesse Squires: Any protocol in Swift that contains optional members must be marked as @objc. I have written before about avoiding @objc in your Swift code as much as possible. When @objc infiltrates your object graph, nearly everything must inherit from NSObject which means you cannot use Swift structs, enums, or other nice features. This [&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":[46,54,71,901],"class_list":["post-18578","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-languagedesign","tag-objective-c","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/18578","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=18578"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/18578\/revisions"}],"predecessor-version":[{"id":18579,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/18578\/revisions\/18579"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=18578"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=18578"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=18578"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}