{"id":9233,"date":"2014-07-25T17:25:56","date_gmt":"2014-07-25T21:25:56","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=9233"},"modified":"2014-08-17T20:40:25","modified_gmt":"2014-08-18T00:40:25","slug":"nil-coalescing-operator-in-swift","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2014\/07\/25\/nil-coalescing-operator-in-swift\/","title":{"rendered":"Nil-coalescing Operator in Swift"},"content":{"rendered":"<p><a href=\"http:\/\/ijoshsmith.com\/2014\/07\/24\/nil-coalescing-operator-in-swift\/\">Josh Smith<\/a>:<\/p>\n<blockquote cite=\"http:\/\/ijoshsmith.com\/2014\/07\/24\/nil-coalescing-operator-in-swift\/\"><p>As seen in the last example above, I created a custom operator function, dubbed the <em>nil-coalescing operator.<\/em> That operator function evaluates to an optional&rsquo;s value unless it is nil, in which case it evaluates to a fallback\/default value. This is based on the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ms173224.aspx\">null-coalescing operator<\/a> in C#, which is formed by two adjacent question marks (<strong>??<\/strong>). Since Swift does not allow custom operator functions to include a question mark, I instead opted for two adjacent exclamation marks (<strong>!!<\/strong>). I think of it as &ldquo;If the optional cannot be unwrapped (because it&rsquo;s nil), then use this value instead.&rdquo;<\/p><\/blockquote>\n<p>Update (2014-08-17): As of beta 5, <a href=\"https:\/\/developer.apple.com\/library\/prerelease\/ios\/documentation\/Swift\/Conceptual\/Swift_Programming_Language\/BasicOperators.html\">Swift now has nil-coalescing operator<\/a>:<\/p>\n<blockquote cite=\"https:\/\/developer.apple.com\/library\/prerelease\/ios\/documentation\/Swift\/Conceptual\/Swift_Programming_Language\/BasicOperators.html\"><p>\n  The <em>nil coalescing operator<\/em> (<code>a ?? b<\/code>) unwraps an optional <code>a<\/code> if it contains a value, or returns a default value <code>b<\/code> if <code>a<\/code> is <code>nil<\/code>. The expression <code>a<\/code> is always of an optional type. The expression <code>b<\/code> must match the type that is stored inside <code>a<\/code>.<\/p><\/blockquote>\n<p><a href=\"http:\/\/airspeedvelocity.net\/2014\/08\/12\/yo-dawg\/\">Airspeed Velocity<\/a>:<\/p>\n<blockquote cite=\"http:\/\/airspeedvelocity.net\/2014\/08\/12\/yo-dawg\/\"><p>Is this behaviour of <code>??<\/code> a bug? I dunno, probably not. You could prefer it to behave like the raw ternary operator, or fail to compile by somehow mandating the right-hand type really be what&rsquo;s contained in the left-hand optional. But you could also say it&rsquo;s behaving correctly, based on how the language works, and you might even need it to behave this way in some scenarios.<\/p>\n<p>Either way, it&rsquo;s a useful case study if you plan on implementing a generic function that takes optionals yourself.<\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Josh Smith: As seen in the last example above, I created a custom operator function, dubbed the nil-coalescing operator. That operator function evaluates to an optional&rsquo;s value unless it is nil, in which case it evaluates to a fallback\/default value. This is based on the null-coalescing operator in C#, which is formed by two adjacent [&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":[71,901],"class_list":["post-9233","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-programming","tag-swift-programming-language"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/9233","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=9233"}],"version-history":[{"count":2,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/9233\/revisions"}],"predecessor-version":[{"id":9310,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/9233\/revisions\/9310"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=9233"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=9233"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=9233"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}