{"id":531,"date":"2018-02-23T23:39:02","date_gmt":"2018-02-23T15:39:02","guid":{"rendered":"http:\/\/60.205.208.74\/2018\/02\/23\/swift-%e6%95%b0%e7%bb%84\/"},"modified":"2018-02-23T23:39:02","modified_gmt":"2018-02-23T15:39:02","slug":"swift-%e6%95%b0%e7%bb%84","status":"publish","type":"post","link":"https:\/\/www.puzi7.net\/?p=531","title":{"rendered":"Swift \u6570\u7ec4"},"content":{"rendered":"<span itemprop=\"description\"><div class=\"article-body\">\n<div class=\"article-intro\" id=\"content\">\n<h1>Swift \u6570\u7ec4<\/h1>\n<p>\nSwift \u6570\u7ec4\u4f7f\u7528\u6709\u5e8f\u5217\u8868\u5b58\u50a8\u540c\u4e00\u7c7b\u578b\u7684\u591a\u4e2a\u503c\u3002\u76f8\u540c\u7684\u503c\u53ef\u4ee5\u591a\u6b21\u51fa\u73b0\u5728\u4e00\u4e2a\u6570\u7ec4\u7684\u4e0d\u540c\u4f4d\u7f6e\u4e2d\u3002<\/p>\n<p>Swift \u6570\u7ec4\u4f1a\u5f3a\u5236\u68c0\u6d4b\u5143\u7d20\u7684\u7c7b\u578b\uff0c\u5982\u679c\u7c7b\u578b\u4e0d\u540c\u5219\u4f1a\u62a5\u9519\uff0cSwift \u6570\u7ec4\u5e94\u8be5\u9075\u5faa\u50cfArray&lt;Element&gt;\u8fd9\u6837\u7684\u5f62\u5f0f\uff0c\u5176\u4e2dElement\u662f\u8fd9\u4e2a\u6570\u7ec4\u4e2d\u552f\u4e00\u5141\u8bb8\u5b58\u5728\u7684\u6570\u636e\u7c7b\u578b\u3002<\/p>\n<p>\u5982\u679c\u521b\u5efa\u4e00\u4e2a\u6570\u7ec4\uff0c\u5e76\u8d4b\u503c\u7ed9\u4e00\u4e2a\u53d8\u91cf\uff0c\u5219\u521b\u5efa\u7684\u96c6\u5408\u5c31\u662f\u53ef\u4ee5\u4fee\u6539\u7684\u3002\u8fd9\u610f\u5473\u7740\u5728\u521b\u5efa\u6570\u7ec4\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u6dfb\u52a0\u3001\u5220\u9664\u3001\u4fee\u6539\u7684\u65b9\u5f0f\u6539\u53d8\u6570\u7ec4\u91cc\u7684\u9879\u76ee\u3002\u5982\u679c\u5c06\u4e00\u4e2a\u6570\u7ec4\u8d4b\u503c\u7ed9\u5e38\u91cf\uff0c\u6570\u7ec4\u5c31\u4e0d\u53ef\u66f4\u6539\uff0c\u5e76\u4e14\u6570\u7ec4\u7684\u5927\u5c0f\u548c\u5185\u5bb9\u90fd\u4e0d\u53ef\u4ee5\u4fee\u6539\u3002<\/p>\n<hr>\n<h2>\u521b\u5efa\u6570\u7ec4<\/h2>\n<p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6784\u9020\u8bed\u6cd5\u6765\u521b\u5efa\u4e00\u4e2a\u7531\u7279\u5b9a\u6570\u636e\u7c7b\u578b\u6784\u6210\u7684\u7a7a\u6570\u7ec4\uff1a<\/p>\n<pre>\nvar someArray = [SomeType]()\n<\/pre>\n<p>\u4ee5\u4e0b\u662f\u521b\u5efa\u4e00\u4e2a\u521d\u59cb\u5316\u5927\u5c0f\u6570\u7ec4\u7684\u8bed\u6cd5\uff1a<\/p>\n<pre>\nvar someArray = [SomeType](repeating: InitialValue, count: NumbeOfElements)\n<\/pre>\n<p>\u4ee5\u4e0b\u5b9e\u4f8b\u521b\u5efa\u4e86\u4e00\u4e2a\u7c7b\u578b\u4e3a Int \uff0c\u6570\u91cf\u4e3a 3\uff0c\u521d\u59cb\u503c\u4e3a 0 \u7684\u7a7a\u6570\u7ec4\uff1a<\/p>\n<pre>\nvar someInts = [Int](repeating: 0, count: 3)\n<\/pre>\n<p>\u4ee5\u4e0b\u5b9e\u4f8b\u521b\u5efa\u4e86\u542b\u6709\u4e09\u4e2a\u5143\u7d20\u7684\u6570\u7ec4\uff1a<\/p>\n<pre>\nvar someInts:[Int] = [10, 20, 30]\n<\/pre>\n<hr>\n<h2>\u8bbf\u95ee\u6570\u7ec4<\/h2>\n<p>\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u6570\u7ec4\u7684\u7d22\u5f15\u6765\u8bbf\u95ee\u6570\u7ec4\u7684\u5143\u7d20\uff0c\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>var someVar = someArray[index]\n<\/pre>\n<p>index \u7d22\u5f15\u4ece 0 \u5f00\u59cb\uff0c\u5373\u7d22\u5f15 0 \u5bf9\u5e94\u7b2c\u4e00\u4e2a\u5143\u7d20\uff0c\u7d22\u5f15 1 \u5bf9\u5e94\u7b2c\u4e8c\u4e2a\u5143\u7d20\uff0c\u4ee5\u6b64\u7c7b\u63a8\u3002<\/p>\n<p>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u5b9e\u4f8b\u6765\u5b66\u4e60\u5982\u4f55\u521b\u5efa\uff0c\u521d\u59cb\u5316\uff0c\u8bbf\u95ee\u6570\u7ec4\uff1a<\/p>\n<pre>\nimport Cocoa\n\nvar someInts = [Int](repeating: 10, count: 3)\n\nvar someVar = someInts[0]\n\nprint( \"\u7b2c\u4e00\u4e2a\u5143\u7d20\u7684\u503c \\(someVar)\" )\nprint( \"\u7b2c\u4e8c\u4e2a\u5143\u7d20\u7684\u503c \\(someInts[1])\" )\nprint( \"\u7b2c\u4e09\u4e2a\u5143\u7d20\u7684\u503c \\(someInts[2])\" )\n<\/pre>\n<p>\u4ee5\u4e0a\u7a0b\u5e8f\u6267\u884c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre>\n\u7b2c\u4e00\u4e2a\u5143\u7d20\u7684\u503c 10\n\u7b2c\u4e8c\u4e2a\u5143\u7d20\u7684\u503c 10\n\u7b2c\u4e09\u4e2a\u5143\u7d20\u7684\u503c 10\n<\/pre>\n<hr>\n<h2>\u4fee\u6539\u6570\u7ec4<\/h2>\n<p>\n\u4f60\u53ef\u4ee5\u4f7f\u7528 append() \u65b9\u6cd5\u6216\u8005\u8d4b\u503c\u8fd0\u7b97\u7b26 += \u5728\u6570\u7ec4\u672b\u5c3e\u6dfb\u52a0\u5143\u7d20\uff0c\u5982\u4e0b\u6240\u793a\uff0c\u6211\u4eec\u521d\u59cb\u5316\u4e00\u4e2a\u6570\u7ec4\uff0c\u5e76\u5411\u5176\u6dfb\u52a0\u5143\u7d20\uff1a<\/p>\n<pre>\nimport Cocoa\n\nvar someInts = [Int]()\n\nsomeInts.append(20)\nsomeInts.append(30)\nsomeInts += [40]\n\nvar someVar = someInts[0]\n\nprint( \"\u7b2c\u4e00\u4e2a\u5143\u7d20\u7684\u503c \\(someVar)\" )\nprint( \"\u7b2c\u4e8c\u4e2a\u5143\u7d20\u7684\u503c \\(someInts[1])\" )\nprint( \"\u7b2c\u4e09\u4e2a\u5143\u7d20\u7684\u503c \\(someInts[2])\" )\n<\/pre>\n<p>\u4ee5\u4e0a\u7a0b\u5e8f\u6267\u884c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre>\n\u7b2c\u4e00\u4e2a\u5143\u7d20\u7684\u503c 20\n\u7b2c\u4e8c\u4e2a\u5143\u7d20\u7684\u503c 30\n\u7b2c\u4e09\u4e2a\u5143\u7d20\u7684\u503c 40\n<\/pre>\n<p>\n\u6211\u4eec\u4e5f\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u4fee\u6539\u6570\u7ec4\u5143\u7d20\u7684\u503c\uff1a<\/p>\n<pre>\nimport Cocoa\n\nvar someInts = [Int]()\n\nsomeInts.append(20)\nsomeInts.append(30)\nsomeInts += [40]\n\n\/\/ \u4fee\u6539\u6700\u540e\u4e00\u4e2a\u5143\u7d20\nsomeInts[2] = 50\n\nvar someVar = someInts[0]\n\nprint( \"\u7b2c\u4e00\u4e2a\u5143\u7d20\u7684\u503c \\(someVar)\" )\nprint( \"\u7b2c\u4e8c\u4e2a\u5143\u7d20\u7684\u503c \\(someInts[1])\" )\nprint( \"\u7b2c\u4e09\u4e2a\u5143\u7d20\u7684\u503c \\(someInts[2])\" )\n<\/pre>\n<p>\u4ee5\u4e0a\u7a0b\u5e8f\u6267\u884c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre>\n\u7b2c\u4e00\u4e2a\u5143\u7d20\u7684\u503c 20\n\u7b2c\u4e8c\u4e2a\u5143\u7d20\u7684\u503c 30\n\u7b2c\u4e09\u4e2a\u5143\u7d20\u7684\u503c 50\n<\/pre>\n<hr>\n<h2>\u904d\u5386\u6570\u7ec4<\/h2>\n<p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528for-in\u5faa\u73af\u6765\u904d\u5386\u6240\u6709\u6570\u7ec4\u4e2d\u7684\u6570\u636e\u9879\uff1a<\/p>\n<pre>\nimport Cocoa\n\nvar someStrs = [String]()\n\nsomeStrs.append(\"Apple\")\nsomeStrs.append(\"Amazon\")\nsomeStrs.append(\"Runoob\")\nsomeStrs += [\"Google\"]\n\nfor item in someStrs {\n   print(item)\n}\n<\/pre>\n<p>\u4ee5\u4e0a\u7a0b\u5e8f\u6267\u884c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre>\nApple\nAmazon\nRunoob\nGoogle\n<\/pre>\n<p>\u5982\u679c\u6211\u4eec\u540c\u65f6\u9700\u8981\u6bcf\u4e2a\u6570\u636e\u9879\u7684\u503c\u548c\u7d22\u5f15\u503c\uff0c\u53ef\u4ee5\u4f7f\u7528 String \u7684 enumerate() \u65b9\u6cd5\u6765\u8fdb\u884c\u6570\u7ec4\u904d\u5386\u3002\u5b9e\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre>\nimport Cocoa\n\nvar someStrs = [String]()\n\nsomeStrs.append(\"Apple\")\nsomeStrs.append(\"Amazon\")\nsomeStrs.append(\"Runoob\")\nsomeStrs += [\"Google\"]\n\nfor (index, item) in someStrs.enumerated() {\n    print(\"\u5728 index = \\(index) \u4f4d\u7f6e\u4e0a\u7684\u503c\u4e3a \\(item)\")\n}\n<\/pre>\n<p>\u4ee5\u4e0a\u7a0b\u5e8f\u6267\u884c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre>\n\u5728 index = 0 \u4f4d\u7f6e\u4e0a\u7684\u503c\u4e3a Apple\n\u5728 index = 1 \u4f4d\u7f6e\u4e0a\u7684\u503c\u4e3a Amazon\n\u5728 index = 2 \u4f4d\u7f6e\u4e0a\u7684\u503c\u4e3a Runoob\n\u5728 index = 3 \u4f4d\u7f6e\u4e0a\u7684\u503c\u4e3a Google\n<\/pre>\n<hr>\n<h2>\u5408\u5e76\u6570\u7ec4<\/h2>\n<p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u52a0\u6cd5\u64cd\u4f5c\u7b26\uff08+\uff09\u6765\u5408\u5e76\u4e24\u79cd\u5df2\u5b58\u5728\u7684\u76f8\u540c\u7c7b\u578b\u6570\u7ec4\u3002\u65b0\u6570\u7ec4\u7684\u6570\u636e\u7c7b\u578b\u4f1a\u4ece\u4e24\u4e2a\u6570\u7ec4\u7684\u6570\u636e\u7c7b\u578b\u4e2d\u63a8\u65ad\u51fa\u6765\uff1a<\/p>\n<pre>\nimport Cocoa\n\nvar intsA = [Int](repeating: 2, count:2)\nvar intsB = [Int](repeating: 1, count:3)\n\nvar intsC = intsA + intsB\n\nfor item in intsC {\n    print(item)\n}\n<\/pre>\n<p>\u4ee5\u4e0a\u7a0b\u5e8f\u6267\u884c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre>\n2\n2\n1\n1\n1\n<\/pre>\n<hr>\n<h2>count \u5c5e\u6027<\/h2>\n<p>\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 count \u5c5e\u6027\u6765\u8ba1\u7b97\u6570\u7ec4\u5143\u7d20\u4e2a\u6570\uff1a<\/p>\n<pre>\nimport Cocoa\n\nvar intsA = [Int](count:2, repeatedValue: 2)\nvar intsB = [Int](count:3, repeatedValue: 1)\n\nvar intsC = intsA + intsB\n\nprint(\"intsA \u5143\u7d20\u4e2a\u6570\u4e3a \\(intsA.count)\")\nprint(\"intsB \u5143\u7d20\u4e2a\u6570\u4e3a \\(intsB.count)\")\nprint(\"intsC \u5143\u7d20\u4e2a\u6570\u4e3a \\(intsC.count)\")\n<\/pre>\n<p>\u4ee5\u4e0a\u7a0b\u5e8f\u6267\u884c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre>\nintsA \u5143\u7d20\u4e2a\u6570\u4e3a 2\nintsB \u5143\u7d20\u4e2a\u6570\u4e3a 3\nintsC \u5143\u7d20\u4e2a\u6570\u4e3a 5\n<\/pre>\n<hr>\n<h2>isEmpty \u5c5e\u6027<\/h2>\n<p>\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u53ea\u8bfb\u5c5e\u6027 isEmpty \u6765\u5224\u65ad\u6570\u7ec4\u662f\u5426\u4e3a\u7a7a\uff0c\u8fd4\u56de\u5e03\u5c14\u503c\uff1a<\/p>\n<pre>\nimport Cocoa\n\nvar intsA = [Int](count:2, repeatedValue: 2)\nvar intsB = [Int](count:3, repeatedValue: 1)\nvar intsC = [Int]()\n\nprint(\"intsA.isEmpty = \\(intsA.isEmpty)\")\nprint(\"intsB.isEmpty = \\(intsB.isEmpty)\")\nprint(\"intsC.isEmpty = \\(intsC.isEmpty)\")\n<\/pre>\n<p>\u4ee5\u4e0a\u7a0b\u5e8f\u6267\u884c\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre>\nintsA.isEmpty = false\nintsB.isEmpty = false\nintsC.isEmpty = true\n<\/pre>\n<\/p><\/div>\n<\/p><\/div>\n<span>","protected":false},"excerpt":{"rendered":"<p class=\"excerpt\"><span itemprop=\"description\">Swift \u6570\u7ec4 Swift \u6570\u7ec4\u4f7f\u7528\u6709\u5e8f\u5217\u8868\u5b58\u50a8\u540c\u4e00\u7c7b\u578b\u7684\u591a\u4e2a\u503c\u3002\u76f8\u540c\u7684\u503c\u53ef\u4ee5\u591a\u6b21\u51fa\u73b0\u5728&hellip;<\/span><\/p>\n<p class=\"more-link-p btn-align-right\"><a class=\"green zoom-btn\" href=\"https:\/\/www.puzi7.net\/?p=531\">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-531","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\/531","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=531"}],"version-history":[{"count":0,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=\/wp\/v2\/posts\/531\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=531"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=531"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.puzi7.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=531"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}