{"id":14672,"date":"2016-05-31T10:13:41","date_gmt":"2016-05-31T14:13:41","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=14672"},"modified":"2016-05-31T10:13:41","modified_gmt":"2016-05-31T14:13:41","slug":"swifty-objective-c","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2016\/05\/31\/swifty-objective-c\/","title":{"rendered":"Swifty Objective-C"},"content":{"rendered":"<p><a href=\"https:\/\/pspdfkit.com\/blog\/2016\/swifty-objective-c\/\">Peter Steinberger, Michael Ochs, and Matej Bukovinski<\/a>:<\/p>\n<blockquote cite=\"https:\/\/pspdfkit.com\/blog\/2016\/swifty-objective-c\/\">\n<p>Using Swift without <a title=\"Winding down the Swift 3 release\" href=\"http:\/\/thread.gmane.org\/gmane.comp.lang.swift.evolution\/17276\">binary compatibility<\/a> would mean that we have to offload technical details to our customers and restrict them in their choice of Xcode to a point where they might not be able to update to Xcode 7.3.1 if our SDK is still compiled with 7.3.0.<\/p>\n<p>[&#8230;]<\/p>\n<p>Instead we decided to use Objective-C++ to complement pure Objective-C where appropriate. [&#8230;]  In our Objective-C classes, we only use a very tiny amount of C++ to benefit from the convenience, safety, and performance features of C++. In contrast to full blown C++ implementations, learning a small subset for use in a mainly Objective-C codebase is very easy, even for developers without any prior C++ experience.<\/p>\n<p>[&#8230;]<\/p>\n<p>When using generics it gets quite annoying to type the type specifier [&#8230;] <code>auto<\/code> will transform at compile time to the above &mdash; it doesn&rsquo;t require any runtime features.<\/p>\n<p>[&#8230;]<\/p>\n<p>In Objective-C <code>NSArray<\/code> can only contain objects. This is both more complicated and &mdash; because of boxing &mdash; slower for primitive types. [&#8230;] With Objective-C++ we can simply use <a title=\"C++ std::vector Reference\" href=\"http:\/\/www.cplusplus.com\/reference\/vector\/vector\/\"><code>std::vector<\/code><\/a>:<\/p>\n<p>[&#8230;]<\/p>\n<p>The C++ lock is automatically released when it goes out of scope. The <a title=\"Resource Acquisition Is Initialization\" href=\"https:\/\/en.wikipedia.org\/wiki\/Resource_Acquisition_Is_Initialization\">RAII<\/a> pattern is everywhere in C++ and it&rsquo;s really great and deterministic. This allows us to do work that requires locking inline with a return statement as the lock is only unlocked after the return.<\/p>\n<p>[&#8230;]<\/p>\n<p>Compiling <code>.mm<\/code> files will take a bit longer than standard <code>.m<\/code> files, however in our experience it&rsquo;s worth the slight compile time penalty.<\/p>\n<\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Peter Steinberger, Michael Ochs, and Matej Bukovinski: Using Swift without binary compatibility would mean that we have to offload technical details to our customers and restrict them in their choice of Xcode to a point where they might not be able to update to Xcode 7.3.1 if our SDK is still compiled with 7.3.0. [&#8230;] [&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":[255,31,54,1007,71,901],"class_list":["post-14672","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-compiler","tag-ios","tag-objective-c","tag-objective-c-plus-plus","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/14672","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=14672"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/14672\/revisions"}],"predecessor-version":[{"id":14673,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/14672\/revisions\/14673"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=14672"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=14672"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=14672"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}