{"id":300,"date":"2018-02-16T13:25:39","date_gmt":"2018-02-16T04:25:39","guid":{"rendered":"http:\/\/windabaft.co.jp\/blog_ceo\/?p=300"},"modified":"2018-02-19T21:35:43","modified_gmt":"2018-02-19T12:35:43","slug":"optional-unwrapp","status":"publish","type":"post","link":"https:\/\/windabaft.co.jp\/blog_ceo\/?p=300","title":{"rendered":"\u30aa\u30d7\u30b7\u30e7\u30ca\u30eb\u578b\u306e\u30a2\u30f3\u30e9\u30c3\u30d7\u306b\u3064\u3044\u3066\u3000Swift4"},"content":{"rendered":"<p><strong><span style=\"color: #ff0000;\">\u30aa\u30d7\u30b7\u30e7\u30ca\u30eb\u578b\uff1d\u5909\u6570\u306bnil\u306e\u4ee3\u5165\u3092\u8a31\u3059\u30c7\u30fc\u30bf\u578b<\/span><\/strong><br \/>\n\u5ba3\u8a00\u65b9\u6cd5\uff1a\u5909\u6570\u306e\u30c7\u30fc\u30bf\u578b\u306e\u6700\u5f8c\u306b&#8221;?&#8221;\u304b&#8221;!&#8221;\u3092\u3064\u3051\u308b<\/p>\n<pre class=\"lang:default decode:true\">var name: String?\r\nvar age: Int!<\/pre>\n<p>\u203b\u30aa\u30d7\u30b7\u30e7\u30ca\u30eb\u578b\u306e\u5909\u6570\u306f\u5ba3\u8a00\u3092\u3057\u305f\u3060\u3051\u3067\u521d\u671f\u5316\u3092\u3057\u306a\u304b\u3063\u305f\u5834\u5408\u3001nil\u304c\u5165\u3063\u3066\u3044\u308b<\/p>\n<p>\u6697\u9ed9\u7684\u30a2\u30f3\u30e9\u30c3\u30d7\u578b&#8221;!&#8221;\u3000\uff1d\u4f7f\u7528\u3059\u308b\u969b\u306b\u81ea\u52d5\u3067\u30a2\u30f3\u30e9\u30c3\u30d7\u3055\u308c\u308b<br \/>\n\u30aa\u30d7\u30b7\u30e7\u30ca\u30eb\u578b\u306e\u5ba3\u8a00\u306b\u306f&#8221;?&#8221;\u304b&#8221;!&#8221;\u3092\u7528\u3044\u307e\u3059\u304c\u3001&#8221;!&#8221;\u3092\u7528\u3044\u3066\u5ba3\u8a00\u3057\u305f\u30aa\u30d7\u30b7\u30e7\u30ca\u30eb\u578b(\u6697\u9ed9\u7684\u30a2\u30f3\u30e9\u30c3\u30d7\u578b)\u306f\u4e0b\u8a18\u306e\u3088\u3046\u306b\u4f7f\u7528\u3059\u308b\u969b\u306b\u81ea\u52d5\u3067\u30a2\u30f3\u30e9\u30c3\u30d7\u3055\u308c\u307e\u3059\u3002<\/p>\n<pre class=\"lang:default decode:true\">\"?\"\u3067\u5ba3\u8a00\u3057\u305f\u30aa\u30d7\u30b7\u30e7\u30ca\u30eb\u578b\u306e\u5834\u5408\u3001\u975e\u30aa\u30d7\u30b7\u30e7\u30ca\u30eb\u578b\u3068\u306f\u8a08\u7b97\u3067\u304d\u306a\u3044\r\nvar x: Int  = 5 \/\/ \u975e\u30aa\u30d7\u30b7\u30e7\u30ca\u30eb\u578b\r\nvar y: Int? = 10 \/\/ \"?\"\u3067\u5ba3\u8a00\u3057\u305f\u30aa\u30d7\u30b7\u30e7\u30ca\u30eb\u578b\r\nx + y\r\n# =&gt; \u30a8\u30e9\u30fc\r\n\r\n\"!\"\u3067\u5ba3\u8a00\u3057\u305f\u30aa\u30d7\u30b7\u30e7\u30ca\u30eb\u578b\u306e\u5834\u5408\u3001\u81ea\u52d5\u3067\u30a2\u30f3\u30e9\u30c3\u30d7\u3055\u308c\u308b\u305f\u3081\u975e\u30aa\u30d7\u30b7\u30e7\u30ca\u30eb\u578b\u3068\u8a08\u7b97\u3067\u304d\u308b\r\nvar x: Int  = 5 \/\/ \u975e\u30aa\u30d7\u30b7\u30e7\u30ca\u30eb\u578b\r\nvar y: Int! = 10 \/\/ \"!\"\u3067\u5ba3\u8a00\u3057\u305f\u30aa\u30d7\u30b7\u30e7\u30ca\u30eb\u578b(\u6697\u9ed9\u7684\u30a2\u30f3\u30e9\u30c3\u30d7\u578b)\r\nx + y\r\n# =&gt; 15<\/pre>\n<p>\u6697\u9ed9\u7684\u30a2\u30f3\u30e9\u30c3\u30d7\u578b\u306f\u3001<\/p>\n<p>\u6700\u521d\u306fnil\u3067\u5ba3\u8a00\u21d2\u4f7f\u7528\u6642\u306b\u306f\u7d76\u5bfe\u306b\u5024\u304c\u5165\u3063\u3066\u3044\u308b\uff08nil\u3067\u306f\u306a\u3044\uff09<\/p>\n<p>\u3068\u3044\u3046\u4f7f\u3044\u65b9\u306e\u969b\u306b\u4f7f\u7528\u3057\u307e\u3059\u3002<br \/>\n\u305d\u306e\u305f\u3081\u3001\u4f7f\u7528\u3059\u308b\u3068\u304d\u306b\u5909\u6570\u306e\u5024\u304cnil\u306e\u3068\u304d\u306b\u306f\u30a8\u30e9\u30fc\u3068\u306a\u308a\u307e\u3059\u306e\u3067\u8981\u6ce8\u610f\u3067\u3059\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong><span style=\"color: #ff0000;\">\u30a2\u30f3\u30e9\u30c3\u30d7\uff1d\u30aa\u30d7\u30b7\u30e7\u30ca\u30eb\u578b\u304b\u3089\u5024\u3092\u53d6\u308a\u3060\u3059<\/span><\/strong><\/p>\n<p>\u30a2\u30f3\u30e9\u30c3\u30d7\u306e\u65b9\u6cd5\u306f\u4e0b\u8a18\uff13\u901a\u308a<\/p>\n<p>1. Forced Unwrapping \uff08\u5f37\u5236\u7684\u30a2\u30f3\u30e9\u30c3\u30d7\uff09<br \/>\n\u30aa\u30d7\u30b7\u30e7\u30ca\u30eb\u578b\u3092\u5f37\u5236\u7684\u306b\u30a2\u30f3\u30e9\u30c3\u30d7\u3059\u308b\u65b9\u6cd5\u3067\u3001<br \/>\n\u30aa\u30d7\u30b7\u30e7\u30ca\u30eb\u578b\u306e\u5f8c\u308d\u306b&#8221;!&#8221;\u3092\u3064\u3051\u308b<\/p>\n<p>2. Optional Binding \uff08\u30aa\u30d7\u30b7\u30e7\u30ca\u30eb\u30d0\u30a4\u30f3\u30c7\u30a3\u30f3\u30b0\uff09<br \/>\n\u30aa\u30d7\u30b7\u30e7\u30ca\u30eb\u578b\u306e\u5909\u6570\u306b\u5024\u304c\u5165\u3063\u3066\u308b\u21d2\u30a2\u30f3\u30e9\u30c3\u30d7\u21d2\u51e6\u7406{}\u3092\u5b9f\u884c<br \/>\n\u30aa\u30d7\u30b7\u30e7\u30ca\u30eb\u578b\u306e\u5909\u6570\u306b\u5024\u304c\u306a\u3044(=nil)\u21d2\u51e6\u7406{}\u3092\u884c\u308f\u306a\u3044\u3001\u3082\u3057\u304f\u306felse{}\u306e\u51e6\u7406\u3092\u5b9f\u884c<\/p>\n<pre class=\"lang:default decode:true\">var test: String? \/\/ \u30aa\u30d7\u30b7\u30e7\u30ca\u30eb\u578b\r\n\r\nif let testUnwrapped = test {\r\nprintln(testUnwrapped)\r\n}\r\nelse {\r\nprintln(\"\u30c6\u30b9\u30c8\u306f\u3042\u308a\u307e\u305b\u3093\")\r\n}\r\n\r\nif let \u5909\u65702 = \u5909\u65701{} \u306fSwift\u3067\u3088\u304f\u4f7f\u308f\u308c\u308bnil\u30c1\u30a7\u30c3\u30af\u306e\u65b9\u6cd5\r\nif let hogehoge = hogehogeValue {\r\n\/\/nil\u3067\u306a\u3044\u5834\u5408\u5b9f\u884c\u3055\u308c\u308b\u51e6\u7406\r\n}<\/pre>\n<p>\u203b\u4ed6\u306b\u3082\u3042\u308b\u30a8\u30e9\u30fc\u30cf\u30f3\u30c9\u30ea\u30f3\u30b0<br \/>\n<strong><span style=\"color: #ff0000;\">if let\u00a0\u5909\u65702 = try? \u5909\u65701 {}<\/span><\/strong><br \/>\n<strong><span style=\"color: #ff0000;\">try?\u306f\u30a8\u30e9\u30fc\u304c\u767a\u751f\u3059\u308b\u3068nil\u3092\u8fd4\u3059\u30aa\u30d7\u30b7\u30e7\u30ca\u30eb\u578b\u5909\u6570<\/span><\/strong>\u3001\u3053\u306e\u7d44\u307f\u5408\u308f\u305b\u3067\u30a8\u30e9\u30fc\u306b\u306a\u3089\u306a\u3044\u3088\u3046\u306b\u3057\u3066\u3044\u307e\u3059<\/p>\n<p>3. Optional Chaining\uff08\u30aa\u30d7\u30b7\u30e7\u30ca\u30eb\u30c1\u30a7\u30a4\u30cb\u30f3\u30b0\uff09<br \/>\n\u30aa\u30d7\u30b7\u30e7\u30ca\u30eb\u578b\u306e\u5909\u6570\u306b\u7d9a\u3051\u3066\u30d7\u30ed\u30d1\u30c6\u30a3\u3092\u53d6\u5f97\u3057\u305f\u308a\u3001\u30e1\u30bd\u30c3\u30c9\u3092\u547c\u3073\u51fa\u3059\u5834\u5408\u306b\u4f7f\u7528\u3059\u308b\u3001<strong><span style=\"color: #ff0000;\">\u623b\u308a\u5024\u306f\u30aa\u30d7\u30b7\u30e7\u30ca\u30eb\u578b\u306b\u306a\u308b<\/span><\/strong>\u3002<br \/>\n\u623b\u308a\u5024\u304c\u30aa\u30d7\u30b7\u30e7\u30ca\u30eb\u578b\u3068\u306a\u308b\u305f\u3081\u3001\u30aa\u30d7\u30b7\u30e7\u30ca\u30eb\u578b\u306e\u5909\u6570\u306e\u4e2d\u8eab\u304cnil\u306e\u53ef\u80fd\u6027\u304c\u3042\u3063\u3066\u3082\u30d7\u30ed\u30d1\u30c6\u30a3\u3092\u53d6\u5f97\u3057\u305f\u308a\u3001\u30e1\u30bd\u30c3\u30c9\u3092\u547c\u3073\u51fa\u305b\u308b\u3002<\/p>\n<p>\u4f7f\u7528\u65b9\u6cd5\uff1a<strong><span style=\"color: #ff0000;\">\u30aa\u30d7\u30b7\u30e7\u30ca\u30eb\u578b\u306e\u5909\u6570\u306e\u3042\u3068\u306b&#8221;?&#8221;\u3092\u3064\u3051\u308b<\/span><\/strong><br \/>\n\u30aa\u30d7\u30b7\u30e7\u30ca\u30eb\u578b\u306e\u5909\u6570?.\u30d7\u30ed\u30d1\u30c6\u30a3<br \/>\n\u30aa\u30d7\u30b7\u30e7\u30ca\u30eb\u578b\u306e\u5909\u6570?.\u30e1\u30bd\u30c3\u30c9()<\/p>\n<p>\u3010\u4f8b1\u3011<\/p>\n<pre class=\"lang:default decode:true\">var animal1: Animal? = Animal()\r\nvar animal2: Animal?\r\n\r\n\/\/ nil\u3067\u306a\u3044\u305f\u3081\u30d7\u30ed\u30d1\u30c6\u30a3\u304c\u53d6\u5f97\u3067\u304d\u308b\r\nanimal1?.type\r\n\/\/ =&gt; \"Optional(\u306d\u3053)\"\r\n\r\n\/\/ nil\u3067\u306a\u3044\u305f\u3081\u30e1\u30bd\u30c3\u30c9\u3092\u547c\u3073\u51fa\u305b\u308b\r\nanimal2?.call()\r\n\/\/ =&gt; \"Optional(\u308f\u3093\u308f\u3093)\"<\/pre>\n<pre class=\"lang:default decode:true \">var animal1: Animal? = Animal()\r\nvar animal2: Animal?\r\n\r\n\/\/ animal1\u304cnil\u306e\u5834\u5408\u3001nil\u304c\u8fd4\u308b\r\nanimal1?.type\r\n\/\/ =&gt; nil\r\n\r\n\/\/ animal2\u304cnil\u306e\u5834\u5408\u3001nil\u304c\u8fd4\u308b\r\nanimal2?.call()\r\n\/\/ =&gt; nil<\/pre>\n<p>\u3010\u4f8b2\u3011<\/p>\n<pre class=\"lang:default decode:true \">var x: String?\r\nVar y = x?.uppercased()\r\n\/\/ =&gt; nil<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u30aa\u30d7\u30b7\u30e7\u30ca\u30eb\u578b\uff1d\u5909\u6570\u306bnil\u306e\u4ee3\u5165\u3092\u8a31\u3059\u30c7\u30fc\u30bf\u578b \u5ba3\u8a00\u65b9\u6cd5\uff1a\u5909\u6570\u306e\u30c7\u30fc\u30bf\u578b\u306e\u6700\u5f8c\u306b&#8221;?&#8221;\u304b&#8221;!&#8221;\u3092\u3064\u3051\u308b var name: String? var age: Int! \u203b &hellip; <a href=\"https:\/\/windabaft.co.jp\/blog_ceo\/?p=300\" class=\"more-link\">\u7d9a\u304d\u3092\u8aad\u3080 <span class=\"screen-reader-text\">\u30aa\u30d7\u30b7\u30e7\u30ca\u30eb\u578b\u306e\u30a2\u30f3\u30e9\u30c3\u30d7\u306b\u3064\u3044\u3066\u3000Swift4<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[22,10],"tags":[],"class_list":["post-300","post","type-post","status-publish","format-standard","hentry","category-swift","category-10"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/windabaft.co.jp\/blog_ceo\/index.php?rest_route=\/wp\/v2\/posts\/300","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/windabaft.co.jp\/blog_ceo\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/windabaft.co.jp\/blog_ceo\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/windabaft.co.jp\/blog_ceo\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/windabaft.co.jp\/blog_ceo\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=300"}],"version-history":[{"count":6,"href":"https:\/\/windabaft.co.jp\/blog_ceo\/index.php?rest_route=\/wp\/v2\/posts\/300\/revisions"}],"predecessor-version":[{"id":328,"href":"https:\/\/windabaft.co.jp\/blog_ceo\/index.php?rest_route=\/wp\/v2\/posts\/300\/revisions\/328"}],"wp:attachment":[{"href":"https:\/\/windabaft.co.jp\/blog_ceo\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=300"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/windabaft.co.jp\/blog_ceo\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=300"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/windabaft.co.jp\/blog_ceo\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=300"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}