{"id":349,"date":"2021-09-17T15:23:07","date_gmt":"1970-01-01T00:00:00","guid":{"rendered":"http:\/\/swift.tecc0.com\/?p=349"},"modified":"2021-09-17T15:23:07","modified_gmt":"1970-01-01T00:00:00","slug":"post-349","status":"publish","type":"post","link":"https:\/\/swift.tecc0.com\/?p=349","title":{"rendered":"Swift\u3067nil coalescing operator(??\u6f14\u7b97\u5b50)\u3092\u4f7f\u3046\u65b9\u6cd5"},"content":{"rendered":"<p>Swift\u3067\u306f<strong>nil coalescing operator(??\u6f14\u7b97\u5b50)<\/strong>\u3068\u3044\u3046\u3082\u306e\u304c\u4f7f\u3048\u308b\u3002<\/p>\n<ul>\n<li>\u5de6\u8fba\u304cnil\u3067\u306f\u306a\u3044\u5834\u5408 -&gt; \u5de6\u8fba\u3092\u4f7f\u3046<\/li>\n<li>\u5de6\u8fba\u304cnil\u3067\u3042\u3063\u305f\u5834\u5408 -&gt; \u53f3\u8fba\u3092\u4f7f\u3046<\/li>\n<\/ul>\n<p>\u3068\u3044\u3046\u51e6\u7406\u3092\u884c\u3048\u308b\u3002<\/p>\n<p>&nbsp;<\/p>\n<h3>Swift\u3067nil coalescing operator(??\u6f14\u7b97\u5b50)\u3092\u4f7f\u3046<\/h3>\n<pre class=\"lang:swift decode:true \">let str1 = \"hoge\"\r\nlet str2: String? = nil\r\n\r\nlet str3: String? = nil\r\nlet str4 = \"fuga\"\r\n\r\nlet result1 = str1 ?? str2\r\nlet result2 = str3 ?? str4\r\n\r\n\/\/ \u5de6\u8fba\u304cnil\u3067\u306f\u306a\u3044\u306e\u3067str1\u3092\u4f7f\u3046\r\nprint(result1) \/\/ Optional(\"hoge\")\r\n\/\/ \u5de6\u8fba\u304cnil\u3067\u3042\u3063\u305f\u306e\u3067str4\u3092\u4f7f\u3046\r\nprint(result2) \/\/ \"fuga\"<\/pre>\n<p>\u3053\u3093\u306a\u611f\u3058\u306b\u306a\u3063\u3066\u3001\u5de6\u8fba\u304cnil\u3067\u3042\u308b\u5834\u5408\u3068\u305d\u3046\u3067\u306a\u3044\u5834\u5408\u3067\u3001\u305d\u308c\u305e\u308c\u53f3\u8fba\u3068\u5de6\u8fba\u304c\u4ee3\u5165\u3055\u308c\u308b\u3002<\/p>\n<p>&nbsp;<\/p>\n<p>\u3061\u306a\u307f\u306b\u51fa\u529b\u7d50\u679c\u306e<strong>Optional<\/strong>\u304c\u6c17\u306b\u306a\u308b\u4eba\u304c\u3044\u308b\u3068\u601d\u3046\u306e\u3067\u3001\u305d\u3053\u3082\u8efd\u304f\u3002<\/p>\n<p>&nbsp;<\/p>\n<h3>nil coalescing operator\u304c\u884c\u3063\u3066\u308b\u51e6\u7406<\/h3>\n<p>\u57fa\u672c\u7684\u306b\u306f\u4e0a\u3067\u66f8\u3044\u305f\u3088\u3046\u306a\u610f\u5473\u306a\u3093\u3060\u3051\u3069\u3001\u5b9f\u969b\u306b\u306f\u4e00\u624b\u9593\u5165\u3063\u3066\u308b\u3002\u9806\u5e8f\u3092\u5165\u308c\u66ff\u3048\u3066\u307f\u3088\u3046\u3002<\/p>\n<pre class=\"lang:swift decode:true\">let str1 = \"hoge\"\r\nlet str2: String? = nil\r\n\r\nprint(str1 ?? str2) \/\/ Optional(\"hoge\")\r\nprint(str2 ?? str1) \/\/ \"hoge\"<\/pre>\n<p>\u5b9f\u969b\u306b\u3084\u3063\u3066\u307f\u308b\u3068\u3001str1\u3068str2\u306e\u9806\u5e8f\u3092\u5165\u308c\u66ff\u3048\u308b\u3060\u3051\u3067\u51fa\u529b\u7d50\u679c\u304c\u7570\u306a\u308b\u3088\u3002<\/p>\n<ul>\n<li>str1\u304cnil\u3067\u306f\u306a\u3044\u5834\u5408(\u4e0a)\u3067\u306f\u3001\u30a2\u30f3\u30e9\u30c3\u30d7\u3057\u3066str1\u3092\u8fd4\u3059<\/li>\n<li>str1\u304cnil\u3067\u3042\u3063\u305f\u5834\u5408(\u4e0b)\u3067\u306f\u3001Optional\u306e\u307e\u307estr2\u3092\u8fd4\u3059<\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p>\u3063\u3066<a href=\"https:\/\/developer.apple.com\/library\/ios\/documentation\/Swift\/Conceptual\/Swift_Programming_Language\/BasicOperators.html\">iOS Developer Library<\/a>\u306b\u66f8\u3044\u3066\u3042\u3063\u305f\u3088\u3002<\/p>\n<p><a href=\"https:\/\/swift.tecc0.com\/wp-content\/uploads\/2016\/05\/634290f8c77161e941209ff01882750f.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-354\" src=\"https:\/\/swift.tecc0.com\/wp-content\/uploads\/2016\/05\/634290f8c77161e941209ff01882750f.png\" alt=\"iOS Developer Library\" width=\"390\" height=\"85\" srcset=\"https:\/\/swift.tecc0.com\/wp-content\/uploads\/2016\/05\/634290f8c77161e941209ff01882750f.png 390w, https:\/\/swift.tecc0.com\/wp-content\/uploads\/2016\/05\/634290f8c77161e941209ff01882750f-300x65.png 300w\" sizes=\"(max-width: 390px) 100vw, 390px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>\u306a\u3093\u304b\u3084\u3084\u3053\u3057\u3044\u3051\u3069\u3001\u3053\u308c\u3067\u7d0d\u5f97\u3057\u3088\u3046\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Swift\u3067\u306fnil coalescing operator(??\u6f14\u7b97\u5b50)\u3068\u3044\u3046\u3082\u306e\u304c\u4f7f\u3048\u308b\u3002 \u5de6\u8fba\u304cnil\u3067\u306f\u306a\u3044\u5834\u5408 -&gt; \u5de6\u8fba\u3092\u4f7f\u3046 \u5de6\u8fba\u304cnil\u3067\u3042\u3063\u305f\u5834\u5408 -&gt; \u53f3\u8fba\u3092\u4f7f\u3046 \u3068\u3044\u3046\u51e6\u7406\u3092\u884c\u3048\u308b\u3002 &#038;&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[17],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/swift.tecc0.com\/index.php?rest_route=\/wp\/v2\/posts\/349"}],"collection":[{"href":"https:\/\/swift.tecc0.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/swift.tecc0.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/swift.tecc0.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/swift.tecc0.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=349"}],"version-history":[{"count":6,"href":"https:\/\/swift.tecc0.com\/index.php?rest_route=\/wp\/v2\/posts\/349\/revisions"}],"predecessor-version":[{"id":356,"href":"https:\/\/swift.tecc0.com\/index.php?rest_route=\/wp\/v2\/posts\/349\/revisions\/356"}],"wp:attachment":[{"href":"https:\/\/swift.tecc0.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=349"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/swift.tecc0.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=349"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/swift.tecc0.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=349"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}