{"id":11546,"date":"2015-06-22T09:39:57","date_gmt":"2015-06-22T13:39:57","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=11546"},"modified":"2015-06-22T09:39:57","modified_gmt":"2015-06-22T13:39:57","slug":"other-new-swift-2-features","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2015\/06\/22\/other-new-swift-2-features\/","title":{"rendered":"Other New Swift 2 Features"},"content":{"rendered":"<p><a href=\"http:\/\/www.hackingwithswift.com\/new-syntax-swift-2-guard\">Paul Hudson<\/a>:<\/p>\n<blockquote cite=\"http:\/\/www.hackingwithswift.com\/new-syntax-swift-2-guard\">\n<p>When a method runs, you want to be sure that it has all the data it needs to work properly, and your code should only execute when that&rsquo;s the case. Coders solved that in common two ways: pyramids of doom and early returns. <\/p>\n<\/blockquote>\n\n<p><a href=\"http:\/\/ericcerney.com\/swift-guard-statement\/\">Eric Cerney<\/a>:<\/p>\n<blockquote cite=\"http:\/\/ericcerney.com\/swift-guard-statement\/\">\n<p>Using <code>guard<\/code> solves all 3 of the issues mentioned above:<\/p>\n<ol>\n<li>Checking for the condition you <i>do<\/i> want, not the one you don&rsquo;t. This again is similar to an <code>assert<\/code>. If the condition is not met, <code>guard<\/code>&lsquo;s <code>else<\/code> statement is run, which breaks out of the function.<\/li>\n<li>If the condition passes, the optional variable here is automatically unwrapped for you within the scope that the <code>guard<\/code> statement was called &#8211; in this case, the <code>fooGuard(_:)<\/code> function. This is an important, yet notably strange feature that really makes the <code>guard<\/code> statement useful.<\/li>\n<li>You are checking for bad cases early, making your function more readable and easier to maintain.<\/li>\n<\/ol>\n<\/blockquote>\n\n<p><a href=\"http:\/\/www.hackingwithswift.com\/new-syntax-swift-2-availability-checking\">Paul Hudson<\/a>:<\/p>\n<blockquote cite=\"http:\/\/www.hackingwithswift.com\/new-syntax-swift-2-availability-checking\">\n<p>In Swift 2, Apple introduced API availability checking. If you set your app&rsquo;s Deployment Target to a lower iOS release than the base SDK, Xcode will automatically scan every API you use to make sure it&rsquo;s available in your lowest deployment target version. This information has been in Apple&rsquo;s API headers for years, but it&rsquo;s only now being exposed to the compiler. What it means is that if your app compiles, you can be guaranteed it doesn&rsquo;t call any code that can&rsquo;t run because of missing APIs.<\/p>\n<p>[&#8230;]<\/p>\n<p>In that code, <code>#available<\/code> is going to check whether we&rsquo;re on iOS 9 or later, or any other unknown platforms like watchOS &#8211; that&rsquo;s the * at the end, and it&rsquo;s required. And that&rsquo;s it: all the code you&rsquo;ll put in place of \"\/\/ use UIStackView\" effectively has elevated rights to use iOS 9.0-only technology, whether that&rsquo;s classes, methods or enums.<\/p>\n<\/blockquote>\n<p><a href=\"http:\/\/mjtsai.com\/blog\/2013\/03\/15\/deploymate-1-0\/\">Finally<\/a>.<\/p>\n\n<p><a href=\"http:\/\/natashatherobot.com\/swift-2-pattern-matching-unwrapping-multiple-optionals\/\">Natasha Murashev<\/a> on unwrapping multiple optionals using pattern matching:<\/p>\n<blockquote cite=\"http:\/\/natashatherobot.com\/swift-2-pattern-matching-unwrapping-multiple-optionals\/\">\n<p>I was at first frustrated by the use of <code>?<\/code> to indicate that the value does exist (especially since I associate it with the idea of optionals, where the value may or may not exist), but I have to admit that this example is very clear to understand. And I definitely like it as a much better alternative to the clunky .Some(username) syntax. <\/p>\n<\/blockquote>\n\n<p><a href=\"http:\/\/chris.eidhof.nl\/posts\/swift-mirrors-and-json.html\">Chris Eidhof<\/a>:<\/p>\n<blockquote cite=\"http:\/\/chris.eidhof.nl\/posts\/swift-mirrors-and-json.html\">\n<p>Inspired by <a href=\"https:\/\/twitter.com\/mikeash\/status\/609575730024984576\">Mike Ash&rsquo;s tweet<\/a>, I tried generating JSON dictionaries in Swift by using the new reflection features.<\/p>\n<p>[&#8230;]<\/p>\n<p>We can provide a default implementation by extending the protocol. This default implementation reflects a value, and loops over the children, recursively serializing them as well. If the type doesn&rsquo;t have any children, we assume it&rsquo;s a primitive (e.g. a String or an Int) and don&rsquo;t serialize it.<\/p>\n<\/blockquote>\n\n<p><a href=\"https:\/\/www.mikeash.com\/pyblog\/friday-qa-2015-06-19-the-best-of-whats-new-in-swift.html\">Mike Ash<\/a>:<\/p>\n<blockquote cite=\"https:\/\/www.mikeash.com\/pyblog\/friday-qa-2015-06-19-the-best-of-whats-new-in-swift.html\"><p>The terrible world of <code>CFunctionPointer<\/code> is gone in Swift 2. Instead, Swift function types have trifurcated into variants. Any Swift function type can optionally be annotated with a <code>@convention<\/code> specifier which says what kind of function it is. The <code>swift<\/code> convention is the default and indicates a normal Swift function. The <code>block<\/code> convention indicates an Objective-C block type. These were automatically bridged, and still are, but now the typing is more explicit. Finally, the <code>c<\/code> convention indicates a C function pointer. Function types annotated with <code>@convention(c)<\/code> still behave mostly normally, so you can call them and pass them as usual.<\/p><\/blockquote>\n\n<p><a href=\"http:\/\/natashatherobot.com\/swift-2-for-in-filtering\/\">Natasha Murashev<\/a>:<\/p>\n<blockquote cite=\"http:\/\/natashatherobot.com\/swift-2-for-in-filtering\/\">\n<p>The idea behind <code>for&#8230;in<\/code> filtering is that now you can do filters inline vs in your <code>for<\/code> loop.<\/p>\n<p>[&#8230;]<\/p>\n<p>Note that you can use the <a href=\"http:\/\/natashatherobot.com\/swift-2-pattern-matching-with-if-case\/\">pattern matching with &ldquo;if case&rdquo;<\/a> in combination with the <code>for&#8230;in<\/code> filtering for some even more advanced filtering!<\/p><\/blockquote>\n<p>I&rsquo;d still like to see list and dictionary comprehensions.<\/p>","protected":false},"excerpt":{"rendered":"<p>Paul Hudson: When a method runs, you want to be sure that it has all the data it needs to work properly, and your code should only execute when that&rsquo;s the case. Coders solved that in common two ways: pyramids of doom and early returns. Eric Cerney: Using guard solves all 3 of the issues [&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":[31,46,30,71,901],"class_list":["post-11546","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-ios","tag-languagedesign","tag-mac","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/11546","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=11546"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/11546\/revisions"}],"predecessor-version":[{"id":11547,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/11546\/revisions\/11547"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=11546"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=11546"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=11546"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}