{"id":18175,"date":"2017-06-14T14:47:14","date_gmt":"2017-06-14T18:47:14","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=18175"},"modified":"2017-06-14T14:47:14","modified_gmt":"2017-06-14T18:47:14","slug":"even-swiftier-objective-c","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2017\/06\/14\/even-swiftier-objective-c\/","title":{"rendered":"Even Swiftier Objective-C"},"content":{"rendered":"<p><a href=\"https:\/\/pspdfkit.com\/blog\/2017\/even-swiftier-objective-c\/\">Peter Steinberger and Matej Bukovinski<\/a>:<\/p>\n<blockquote cite=\"https:\/\/pspdfkit.com\/blog\/2017\/even-swiftier-objective-c\/\">\n<p>In our original <a href=\"\/blog\/2016\/swifty-objective-c\/\">&ldquo;Swifty Objective-C&rdquo;<\/a> blog post we talked about the C++ <code>auto<\/code> keyword and how it&rsquo;s great for preserving type information while also making the code more readable and easier to write. This is especially true when dealing with generics or block types. Since then, Objective-C learned the same trick via the new <code>__auto_type<\/code> keyword. Since nobody wants to type <code>__auto_type<\/code> all day long, we decided to make things nicer by defining Swift-like macros for it.<\/p>\n<p>[&#8230;]<\/p>\n<p>There&rsquo;s only one <code>copy<\/code> selector, and it&rsquo;s not generic. It&rsquo;s also not a case where returning <code>instancetype<\/code> would be the right fix, as there&rsquo;s no general way to understand what a mutable counterpart of a class pair is called and if one even exists. [&#8230;] However, we can just add that ourselves! Again, a header-only declaration that simply redefines <code>copy<\/code> on our collections. This won&rsquo;t magically add types to every single object, but it will solve the common case of (mutable) copying collections<\/p>\n<p>[&#8230;]<\/p>\n<p>We define a block and add <code>__attribute__((cleanup))<\/code> to it that tells the compiler to execute the function defined in that attribute. We pass along the block as a parameter and thus execute the block when the scope is exited.<\/p>\n<\/blockquote>\n<p>Previously: <a href=\"https:\/\/mjtsai.com\/blog\/2016\/05\/31\/swifty-objective-c\/\">Swifty Objective-C<\/a>, <a href=\"https:\/\/mjtsai.com\/blog\/2017\/05\/08\/foreach-using-objective-c-generics\/\">foreach Using Objective-C Generics<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>Peter Steinberger and Matej Bukovinski: In our original &ldquo;Swifty Objective-C&rdquo; blog post we talked about the C++ auto keyword and how it&rsquo;s great for preserving type information while also making the code more readable and easier to write. This is especially true when dealing with generics or block types. Since then, Objective-C learned the same [&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":[2],"tags":[69,31,1380,30,1381,54,71,901],"class_list":["post-18175","post","type-post","status-publish","format-standard","hentry","category-technology","tag-cocoa","tag-ios","tag-ios-10","tag-mac","tag-macos-10-12","tag-objective-c","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/18175","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=18175"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/18175\/revisions"}],"predecessor-version":[{"id":18176,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/18175\/revisions\/18176"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=18175"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=18175"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=18175"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}