{"id":523,"date":"2018-02-23T23:38:59","date_gmt":"2018-02-23T15:38:59","guid":{"rendered":"http:\/\/60.205.208.74\/2018\/02\/23\/swift-for-%e5%be%aa%e7%8e%af\/"},"modified":"2018-02-23T23:38:59","modified_gmt":"2018-02-23T15:38:59","slug":"swift-for-%e5%be%aa%e7%8e%af","status":"publish","type":"post","link":"https:\/\/www.puzi7.net\/?p=523","title":{"rendered":"Swift for \u5faa\u73af"},"content":{"rendered":"<span itemprop=\"description\"><div class=\"article-body\">\n<div class=\"article-intro\" id=\"content\">\n<h1>Swift for \u5faa\u73af<\/h1>\n<p><a href=\"\/swift-loops.html\"><img decoding=\"async\" class=\"navup\" src=\"\/images\/up.gif\" alt=\"Swift \u5faa\u73af\">Swift \u5faa\u73af<\/a><\/p>\n<blockquote>\n<p style=\"font-size:16px;\">\n<span class=\"deprecated\"><br \/>\n<i class=\"fa fa-thumbs-o-down\" aria-hidden=\"true\"><\/i><br \/>\n\u8be5\u5faa\u73af\u65b9\u5f0f\u5728 Swift 3 \u4e2d\u5df2\u7ecf\u5f03\u7528\u3002<\/span>\n<\/p>\n<\/blockquote>\n<p>Swift for \u5faa\u73af\u7528\u6765\u91cd\u590d\u6267\u884c\u4e00\u7cfb\u5217\u8bed\u53e5\u76f4\u5230\u8fbe\u6210\u7279\u5b9a\u6761\u4ef6\uff0c\u4e00\u822c\u901a\u8fc7\u5728\u6bcf\u6b21\u5faa\u73af\u5b8c\u6210\u540e\u589e\u52a0\u8ba1\u6570\u5668\u7684\u503c\u6765\u5b9e\u73b0\u3002<\/p>\n<h3>\u8bed\u6cd5<\/h3>\n<p>Swift for \u5faa\u73af\u7684\u8bed\u6cd5\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n<pre>\nfor init; condition; increment{\n   \u5faa\u73af\u4f53\n}\n<\/pre>\n<p><b>\u53c2\u6570\u89e3\u6790\uff1a<\/b><\/p>\n<ol class=\"list\">\n<li><b>init<\/b> \u4f1a\u9996\u5148\u88ab\u6267\u884c\uff0c\u4e14\u53ea\u4f1a\u6267\u884c\u4e00\u6b21\u3002\u8fd9\u4e00\u6b65\u5141\u8bb8\u60a8\u58f0\u660e\u5e76\u521d\u59cb\u5316\u4efb\u4f55\u5faa\u73af\u63a7\u5236\u53d8\u91cf\u3002\u60a8\u4e5f\u53ef\u4ee5\u4e0d\u5728\u8fd9\u91cc\u5199\u4efb\u4f55\u8bed\u53e5\uff0c\u53ea\u8981\u6709\u4e00\u4e2a\u5206\u53f7\u51fa\u73b0\u5373\u53ef\u3002<\/li>\n<li>\u63a5\u4e0b\u6765\uff0c\u4f1a\u5224\u65ad <b>condition<\/b>\u3002\u5982\u679c\u4e3a\u771f\uff0c\u5219\u6267\u884c\u5faa\u73af\u4e3b\u4f53\u3002\u5982\u679c\u4e3a\u5047\uff0c\u5219\u4e0d\u6267\u884c\u5faa\u73af\u4e3b\u4f53\uff0c\u4e14\u63a7\u5236\u6d41\u4f1a\u8df3\u8f6c\u5230\u7d27\u63a5\u7740 for \u5faa\u73af\u7684\u4e0b\u4e00\u6761\u8bed\u53e5\u3002<\/li>\n<li>\u5728\u6267\u884c\u5b8c for \u5faa\u73af\u4e3b\u4f53\u540e\uff0c\u63a7\u5236\u6d41\u4f1a\u8df3\u56de\u4e0a\u9762\u7684 <b>increment<\/b> \u8bed\u53e5\u3002\u8be5\u8bed\u53e5\u5141\u8bb8\u60a8\u66f4\u65b0\u5faa\u73af\u63a7\u5236\u53d8\u91cf\u3002\u8be5\u8bed\u53e5\u53ef\u4ee5\u7559\u7a7a\uff0c\u53ea\u8981\u5728\u6761\u4ef6\u540e\u6709\u4e00\u4e2a\u5206\u53f7\u51fa\u73b0\u5373\u53ef\u3002<\/li>\n<li>\u6761\u4ef6\u518d\u6b21\u88ab\u5224\u65ad\u3002\u5982\u679c\u4e3a\u771f\uff0c\u5219\u6267\u884c\u5faa\u73af\uff0c\u8fd9\u4e2a\u8fc7\u7a0b\u4f1a\u4e0d\u65ad\u91cd\u590d\uff08\u5faa\u73af\u4e3b\u4f53\uff0c\u7136\u540e\u589e\u52a0\u6b65\u503c\uff0c\u518d\u7136\u540e\u91cd\u65b0\u5224\u65ad\u6761\u4ef6\uff09\u3002\u5728\u6761\u4ef6\u53d8\u4e3a\u5047\u65f6\uff0cfor \u5faa\u73af\u7ec8\u6b62\u3002<\/li>\n<\/ol>\n<p><b>\u6d41\u7a0b\u56fe\uff1a<\/b><\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.runoob.com\/wp-content\/uploads\/2015\/11\/swift_for_loop.jpg\"><\/p>\n<h3>\u5b9e\u4f8b<\/h3>\n<pre>\nimport Cocoa\n\nvar someInts:[Int] = [10, 20, 30]\n\nfor var index = 0; index &lt; 3; ++index {\n   print( \"\u7d22\u5f15 [\\(index)] \u5bf9\u5e94\u7684\u503c\u4e3a \\(someInts[index])\")\n}\n<\/pre>\n<p>\u4ee5\u4e0a\u7a0b\u5e8f\u6267\u884c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre>\n\u7d22\u5f15 [0] \u5bf9\u5e94\u7684\u503c\u4e3a 10\n\u7d22\u5f15 [1] \u5bf9\u5e94\u7684\u503c\u4e3a 20\n\u7d22\u5f15 [2] \u5bf9\u5e94\u7684\u503c\u4e3a 30\n<\/pre>\n<p><a href=\"\/swift-loops.html\"><img decoding=\"async\" class=\"navup\" src=\"\/images\/up.gif\" alt=\"Swift \u5faa\u73af\">Swift \u5faa\u73af<\/a><\/p>\n<\/p><\/div>\n<\/p><\/div>\n<span>","protected":false},"excerpt":{"rendered":"<p class=\"excerpt\"><span itemprop=\"description\">Swift for \u5faa\u73af Swift \u5faa\u73af \u8be5\u5faa\u73af\u65b9\u5f0f\u5728 Swift 3 \u4e2d\u5df2\u7ecf\u5f03\u7528\u3002 S&hellip;<\/span><\/p>\n<p class=\"more-link-p btn-align-right\"><a class=\"green zoom-btn\" href=\"https:\/\/www.puzi7.net\/?p=523\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26,28],"tags":[74,119],"class_list":["post-523","post","type-post","status-publish","format-standard","hentry","category-mobile","category-swift","tag-ios","tag-swift","zoom-theme-has-thumb"],"_links":{"self":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/posts\/523","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=523"}],"version-history":[{"count":0,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/posts\/523\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=523"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=523"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=523"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}