{"id":16561,"date":"2016-12-07T19:16:58","date_gmt":"2016-12-08T00:16:58","guid":{"rendered":"http:\/\/mjtsai.com\/blog\/?p=16561"},"modified":"2016-12-07T19:16:58","modified_gmt":"2016-12-08T00:16:58","slug":"swift-optionals-and-string-interpolation","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2016\/12\/07\/swift-optionals-and-string-interpolation\/","title":{"rendered":"Swift Optionals and String Interpolation"},"content":{"rendered":"<p><a href=\"https:\/\/oleb.net\/blog\/2016\/12\/optionals-string-interpolation\/\">Ole Begemann<\/a>:<\/p>\n<blockquote cite=\"https:\/\/oleb.net\/blog\/2016\/12\/optionals-string-interpolation\/\">\n<p>Do you know this problem? You want to display an optional value in the UI or log it to the console for debugging, but you don&rsquo;t like the default string conversion for optionals, which results in either <code>\"Optional(&#8230;)\"<\/code> or <code>\"nil\"<\/code>.<\/p>\n<p>[&#8230;]<\/p>\n<p>The problem with the third option is that the <a href=\"https:\/\/developer.apple.com\/library\/content\/documentation\/Swift\/Conceptual\/Swift_Programming_Language\/BasicOperators.html#\/\/apple_ref\/doc\/uid\/TP40014097-CH6-ID72\" title=\"The Swift Programming Language: Nil-Coalescing Operator\">nil-coalescing operator<\/a> <code>??<\/code> requires matching types &mdash; if the left operand is a <code>T?<\/code>, the right operand must be a <code>T<\/code>. Applied to the example above, this means I can provide another <code>Int<\/code> as a default value, but not a string &mdash; which is what I&rsquo;d like to do in this situation.<\/p>\n<p>[&#8230;]<\/p>\n<p>I solved this by defining my own custom optional-string-coalescing operator.<\/p>\n<\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Ole Begemann: Do you know this problem? You want to display an optional value in the UI or log it to the console for debugging, but you don&rsquo;t like the default string conversion for optionals, which results in either \"Optional(&#8230;)\" or \"nil\". [&#8230;] The problem with the third option is that the nil-coalescing operator ?? [&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-16561","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\/16561","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=16561"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/16561\/revisions"}],"predecessor-version":[{"id":16562,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/16561\/revisions\/16562"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=16561"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=16561"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=16561"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}