{"id":266,"date":"2025-06-03T10:38:13","date_gmt":"2025-06-05T03:17:13","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=266"},"modified":"2025-06-05T10:38:13","modified_gmt":"2025-06-05T03:17:13","slug":"swift-array%e6%89%be%e5%80%bc-%e5%be%9e%e9%9b%86%e5%90%88%e6%89%be%e5%b0%8b%e6%88%90%e5%93%a1-firstindex-contains-filter","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/swift-array%e6%89%be%e5%80%bc-%e5%be%9e%e9%9b%86%e5%90%88%e6%89%be%e5%b0%8b%e6%88%90%e5%93%a1-firstindex-contains-filter\/","title":{"rendered":"Swift \u7a0b\u5f0f\u6559\u5b78\uff1a\u5982\u4f55\u5728 Array \u4e2d\u67e5\u627e\u5143\u7d20 | \u4f7f\u7528 contains\u3001firstIndex \u548c filter \u65b9\u6cd5"},"content":{"rendered":"<p>## Swift \u7a0b\u5f0f\u6559\u5b78\uff1a\u5982\u4f55\u5728 Array \u4e2d\u67e5\u627e\u5143\u7d20<\/p>\n<p>\u5728 Swift \u4e2d\uff0c\u67e5\u627e Array \u4e2d\u7684\u5143\u7d20\u662f\u4e00\u500b\u5e38\u898b\u7684\u64cd\u4f5c\u3002\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528 `contains`\u3001`firstIndex(of:)` \u548c `filter` \u65b9\u6cd5\u4f86\u67e5\u627e\u5143\u7d20\uff0c\u4e26\u63d0\u4f9b2025\u5e74\u6700\u65b0\u7684\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10\u3002<\/p>\n<p>### \u6e2c\u8a66\u8cc7\u6599 Array<\/p>\n<p>\u6211\u5011\u5c07\u4f7f\u7528\u4ee5\u4e0b\u6e2c\u8a66\u8cc7\u6599\u4f86\u9032\u884c\u6f14\u793a\uff1a<\/p>\n<p>&#8220;`swift<br \/>\nvar testList = [&#8220;\u6e2c\u8a661&#8221;, &#8220;\u6e2c\u8a662&#8221;, &#8220;\u6e2c\u8a663&#8221;, &#8220;\u6e2c\u8a664&#8221;, &#8220;\u6e2c\u8a665&#8221;]\n&#8220;`<\/p>\n<p>### \u5c0b\u627e\u503c\u4e26\u56de\u50b3 Bool<\/p>\n<p>\u4f7f\u7528 `contains` \u65b9\u6cd5\u53ef\u4ee5\u7c21\u55ae\u5730\u6aa2\u67e5 Array \u4e2d\u662f\u5426\u5b58\u5728\u67d0\u500b\u503c\uff0c\u8fd4\u56de\u4e00\u500b Boolean \u503c\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528\u7bc4\u4f8b\uff1a<\/p>\n<p>&#8220;`swift<br \/>\nif testList.contains(&#8220;\u6e2c\u8a662&#8221;) {<br \/>\n    print(&#8220;\u627e\u5230\u4e86&#8221;)<br \/>\n} else {<br \/>\n    print(&#8220;\u627e\u4e0d\u5230&#8221;)<br \/>\n}<br \/>\n&#8220;`<\/p>\n<p>### \u5c0b\u627e\u503c\u4e26\u56de\u50b3 Index<\/p>\n<p>\u5982\u679c\u60a8\u9700\u8981\u77e5\u9053\u7279\u5b9a\u503c\u5728 Array \u4e2d\u7684\u4f4d\u7f6e\uff0c\u53ef\u4ee5\u4f7f\u7528 `firstIndex(of:)` \u65b9\u6cd5\u3002\u9019\u500b\u65b9\u6cd5\u5c07\u8fd4\u56de\u8a72\u503c\u7684\u7d22\u5f15\uff08\u5982\u679c\u5b58\u5728\uff09\uff0c\u82e5\u4e0d\u5b58\u5728\u5247\u8fd4\u56de `nil`\u3002\u4ee5\u4e0b\u662f\u793a\u7bc4\u4ee3\u78bc\uff1a<\/p>\n<p>&#8220;`swift<br \/>\nif let index = testList.firstIndex(of: &#8220;\u6e2c\u8a662&#8221;) {<br \/>\n    print(&#8220;\u6e2c\u8a662 \u7684\u7d22\u5f15\u662f\uff1a\\(index)&#8221;)<br \/>\n} else {<br \/>\n    print(&#8220;\u627e\u4e0d\u5230\u6e2c\u8a662&#8221;)<br \/>\n}<br \/>\n&#8220;`<\/p>\n<p>### \u4f7f\u7528 filter \u65b9\u6cd5\u67e5\u627e\u591a\u500b\u5143\u7d20<\/p>\n<p>\u82e5\u60a8\u5e0c\u671b\u67e5\u627e\u7b26\u5408\u7279\u5b9a\u689d\u4ef6\u7684\u6240\u6709\u5143\u7d20\uff0c\u53ef\u4ee5\u4f7f\u7528 `filter` \u65b9\u6cd5\u3002\u9019\u500b\u65b9\u6cd5\u6703\u8fd4\u56de\u4e00\u500b\u65b0\u7684 Array\uff0c\u5305\u542b\u6240\u6709\u6eff\u8db3\u689d\u4ef6\u7684\u5143\u7d20\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528\u5b83\u7684\u7bc4\u4f8b\uff1a<\/p>\n<p>&#8220;`swift<br \/>\nlet filteredList = testList.filter { $0.contains(&#8220;\u6e2c\u8a66&#8221;) }<br \/>\nprint(&#8220;\u7b26\u5408\u689d\u4ef6\u7684\u5143\u7d20\u6709\uff1a\\(filteredList)&#8221;)<br \/>\n&#8220;`<\/p>\n<p>### \u932f\u8aa4\u6392\u9664<\/p>\n<p>1. **\u627e\u4e0d\u5230\u5143\u7d20**\uff1a\u78ba\u4fdd\u60a8\u67e5\u627e\u7684\u5143\u7d20\u78ba\u5be6\u5b58\u5728\u65bc Array \u4e2d\u3002\u5982\u679c\u8fd4\u56de `nil`\uff0c\u53ef\u4ee5\u6aa2\u67e5\u62fc\u5beb\u6216\u5927\u5c0f\u5beb\u3002<br \/>\n2. **\u7a7a Array**\uff1a\u5982\u679c Array \u70ba\u7a7a\uff0c\u6240\u6709\u67e5\u627e\u64cd\u4f5c\u90fd\u5c07\u8fd4\u56de `nil` \u6216 `false`\u3002\u52d9\u5fc5\u5728\u67e5\u627e\u524d\u78ba\u8a8d Array \u662f\u5426\u5305\u542b\u5143\u7d20\u3002<\/p>\n<p>### \u5ef6\u4f38\u61c9\u7528<\/p>\n<p>&#8211; \u4f7f\u7528 `contains` \u548c `firstIndex(of:)` \u65b9\u6cd5\u53ef\u4ee5\u5728\u7528\u6236\u8f38\u5165\u7684\u8cc7\u6599\u4e2d\u9032\u884c\u5feb\u901f\u67e5\u627e\uff0c\u9069\u5408\u7528\u65bc\u8868\u55ae\u9a57\u8b49\u3002<br \/>\n&#8211; `filter` \u65b9\u6cd5\u9069\u5408\u7528\u65bc\u8cc7\u6599\u7be9\u9078\u529f\u80fd\uff0c\u4f8b\u5982\u986f\u793a\u7b26\u5408\u7279\u5b9a\u689d\u4ef6\u7684\u8cc7\u6599\u5217\u8868\u3002<\/p>\n<p>![Array \u67e5\u627e\u793a\u7bc4\u5716](https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2020\/09\/wp_editor_md_89212c9d8e76dbd70f13195f5db3fc1e.jpg)<\/p>\n<p>&#8212;<\/p>\n<p>## Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/p>\n<p>**Q1: \u5982\u4f55\u5728 Swift \u4e2d\u5224\u65b7\u4e00\u500b Array \u662f\u5426\u70ba\u7a7a\uff1f**<br \/>\nA1: \u60a8\u53ef\u4ee5\u4f7f\u7528 `isEmpty` \u5c6c\u6027\u4f86\u5224\u65b7 Array \u662f\u5426\u70ba\u7a7a\uff0c\u4f8b\u5982\uff1a`if testList.isEmpty { print(&#8220;Array \u662f\u7a7a\u7684&#8221;) }`\u3002<\/p>\n<p>**Q2: `firstIndex(of:)` \u65b9\u6cd5\u5728 Array \u4e2d\u7684\u6027\u80fd\u5982\u4f55\uff1f**<br \/>\nA2: `firstIndex(of:)` \u65b9\u6cd5\u7684\u6027\u80fd\u53d6\u6c7a\u65bc Array \u7684\u5927\u5c0f\uff0c\u56e0\u70ba\u5b83\u9700\u8981\u904d\u6b77 Array \u4f86\u67e5\u627e\u5143\u7d20\uff0c\u6642\u9593\u8907\u96dc\u5ea6\u70ba O(n)\u3002<\/p>\n<p>**Q3: \u9664\u4e86 `contains` \u548c `firstIndex(of:)`\uff0c\u9084\u6709\u5176\u4ed6\u67e5\u627e\u5143\u7d20\u7684\u65b9\u6cd5\u55ce\uff1f**<br \/>\nA3: \u662f\u7684\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 `filter` \u65b9\u6cd5\u4f86\u67e5\u627e\u7b26\u5408\u689d\u4ef6\u7684\u6240\u6709\u5143\u7d20\uff0c\u4e5f\u53ef\u4ee5\u4f7f\u7528 `forEach` \u5faa\u74b0\u4f86\u9010\u4e00\u6aa2\u67e5\u5143\u7d20\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Array\u627e\u503c\/\u5f9e\u96c6\u5408\u627e\u5c0b\u6210\u54e1<br \/>\n\u55ae\u7d14\u5c0b\u627eArray\u88e1\u9762\u662f\u5426\u6709\u503c\u56de\u50b3Bool<br \/>\n\u5c0b\u627eArray\u88e1\u9762\u662f\u5426\u6709\u503c\u56de\u50b3Index<\/p>\n","protected":false},"author":1,"featured_media":559,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"aside","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[5],"tags":[59,57,58,56,60,61],"class_list":["post-266","post","type-post","status-publish","format-aside","has-post-thumbnail","hentry","category-swift","tag-array","tag-contains","tag-filter","tag-firstindex","tag-list","tag-61","post_format-post-format-aside"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2020\/11\/cup-1-1.png","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-4i","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/266","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=266"}],"version-history":[{"count":4,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/266\/revisions"}],"predecessor-version":[{"id":1159,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/266\/revisions\/1159"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/559"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=266"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=266"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=266"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}