{"id":375,"date":"2021-09-17T15:23:07","date_gmt":"1970-01-01T00:00:00","guid":{"rendered":"http:\/\/swift.tecc0.com\/?p=375"},"modified":"2021-09-17T15:23:07","modified_gmt":"1970-01-01T00:00:00","slug":"post-375","status":"publish","type":"post","link":"https:\/\/swift.tecc0.com\/?p=375","title":{"rendered":"Swift\u3067guard\u3092\u4f7f\u3046\u65b9\u6cd5(nil, \u30a2\u30f3\u30e9\u30c3\u30d7)"},"content":{"rendered":"<p><strong>guard<\/strong>\u306fSwift2.0\u304b\u3089\u53d6\u308a\u5165\u308c\u3089\u308c\u305f\u3001\u6bd4\u8f03\u7684\u65b0\u3057\u3044\u3082\u306e\u3067\u3059\u3002<\/p>\n<p>\u5fc5\u9808\u6761\u4ef6\u3092\u66f8\u3044\u3066\u3001\u4e00\u822c\u7684\u306b\u306f\u305d\u308c\u304c\u6210\u7acb\u3057\u306a\u3044\u5834\u5408\u306breturn\u306a\u3069\u3067\u95a2\u6570\u304b\u3089\u629c\u3051\u305f\u308a\u3057\u307e\u3059\u3002<\/p>\n<h3>Swift\u3067guard\u3092\u4f7f\u3046<\/h3>\n<pre class=\"lang:swift decode:true\">func printIf10Over (x: Int) {\n    \/\/ 10\u4ee5\u4e0a\u3067\u306f\u306a\u304f\u3001\u6761\u4ef6\u3092\u6e80\u305f\u3055\u306a\u3044\u306e\u3067return\n    guard x &gt;= 10 else { return }    \n    print(x)\n}\n\nprintIf10Over(x: 4) \/\/\nprintIf10Over(x: 13) \/\/ 13<\/pre>\n<p>10\u4ee5\u4e0a\u3060\u3063\u305f\u5834\u5408\u306bprint\u3059\u308b\u95a2\u6570\u3067\u3001\u6700\u521d\u306b<strong>guard\u6587<\/strong>\u306710\u672a\u6e80\u306e\u3082\u306e\u3092\u9664\u5916\u3057\u3066\u3044\u308b\u3002<\/p>\n<p>\u3044\u308f\u3086\u308b\u3001<strong>\u65e9\u671f\u30ea\u30bf\u30fc\u30f3<\/strong>\u3068\u3044\u3046\u65b9\u6cd5\u3067\u3088\u304f\u4f7f\u308f\u308c\u308b\u3002<\/p>\n<h3>guard\u6587\u306e\u826f\u3044\u3068\u3053\u308d<\/h3>\n<p>\u4f8b\u3048\u3070\u3001if\u6587\u3067\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u66f8\u304f\u3053\u3068\u3082\u3082\u3061\u308d\u3093\u3067\u304d\u308b<\/p>\n<pre class=\"lang:swift decode:true\">func printIf10Over (x: Int) {\n    \/\/ 10\u4ee5\u4e0a\u3067\u306f\u306a\u304f\u3001\u6761\u4ef6\u3092\u6e80\u305f\u3055\u306a\u3044\u306e\u3067return\n    if !(x &gt;= 10) { return }\n    print(x)\n}\n\nprintIf10Over(x: 4) \/\/\nprintIf10Over(x: 13) \/\/ 13<\/pre>\n<p>\u7d50\u679c\u7684\u306b\u306f\u540c\u3058\u3002<\/p>\n<p>\u4f8b\u3048\u3070\u3001\u305d\u308c\u305e\u308c\u306breturn\u304c\u3042\u308b\u304c\u3001\u3053\u308c\u3092\u30b3\u30e1\u30f3\u30c8\u30a2\u30a6\u30c8\u3059\u308b\u3068<strong>guard\u6587\u3067\u306f\u30a8\u30e9\u30fc\u306b\u306a\u308b<\/strong>\u3002<\/p>\n<p><span style=\"color: #ff0000;\">\u65e9\u671f\u30ea\u30bf\u30fc\u30f3\u3067return\u3092\u66f8\u304d\u5fd8\u308c\u308b\u306e\u306f\u826f\u304f\u3042\u308b<\/span>\u3053\u3068\u3067\u3001\u305d\u308c\u306e\u4e88\u9632\u306b\u3082<strong>guard\u306f\u6709\u52b9<\/strong>\u3002<\/p>\n<p>\u4ed6\u306b\u306f<\/p>\n<ul>\n<li>break<\/li>\n<li>continue<\/li>\n<li>throw<\/li>\n<\/ul>\n<p>\u306a\u3093\u304b\u3082\u4f7f\u3048\u308b\u306e\u3067\u3001\u3046\u307e\u304f\u4f7f\u3063\u3066\u307f\u3088\u3046\u3002<\/p>\n<h3>guard\u3067nil\u30c1\u30a7\u30c3\u30af, \u30a2\u30f3\u30e9\u30c3\u30d7\u3092\u3059\u308b<\/h3>\n<pre class=\"lang:swift decode:true \">func printNum (num: Int?) {\n    guard let unwrappedNum = num else {\n        print(\"nil\")\n        return\n    }\n    \n    print(num)\n    print(unwrappedNum)\n}\n\nprintNum(num: 13)\n\/\/ Optional(13)\n\/\/ 13\nprintNum(num: nil)\n\/\/ nil<\/pre>\n<p>\u3053\u3093\u306a\u611f\u3058\u3067\u66f8\u3044\u3066\u3042\u3052\u308c\u3070\u3001num\u3092\u30a2\u30f3\u30e9\u30c3\u30d7\u3057\u3066\u304f\u308c\u305f\u308a\u3001nil\u3060\u3063\u305f\u5834\u5408\u306b\u51e6\u7406\u3092\u4e2d\u65ad\u3055\u305b\u305f\u308a\u3067\u304d\u308b\u3088\u3002<\/p>\n<p>\u3053\u3061\u3089\u3082if\u6587\u3067\u66f8\u3051\u308b\u3051\u3069\u3001guard\u306e\u65b9\u304c\u4f55\u3092\u3057\u3066\u308b\u304b\u4e00\u76ee\u77ad\u7136\u306a\u306e\u3067\u7a4d\u6975\u7684\u306b\u4f7f\u3063\u3066\u3044\u3053\u3046\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>guard\u306fSwift2.0\u304b\u3089\u53d6\u308a\u5165\u308c\u3089\u308c\u305f\u3001\u6bd4\u8f03\u7684\u65b0\u3057\u3044\u3082\u306e\u3067\u3059\u3002 \u5fc5\u9808\u6761\u4ef6\u3092\u66f8\u3044\u3066\u3001\u4e00\u822c\u7684\u306b\u306f\u305d\u308c\u304c\u6210\u7acb\u3057\u306a\u3044\u5834\u5408\u306breturn\u306a\u3069\u3067\u95a2\u6570\u304b\u3089\u629c\u3051\u305f\u308a\u3057\u307e\u3059\u3002 Swift\u3067guard\u3092\u4f7f\u3046 func printIf&#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\/375"}],"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=375"}],"version-history":[{"count":5,"href":"https:\/\/swift.tecc0.com\/index.php?rest_route=\/wp\/v2\/posts\/375\/revisions"}],"predecessor-version":[{"id":829,"href":"https:\/\/swift.tecc0.com\/index.php?rest_route=\/wp\/v2\/posts\/375\/revisions\/829"}],"wp:attachment":[{"href":"https:\/\/swift.tecc0.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=375"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/swift.tecc0.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=375"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/swift.tecc0.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=375"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}