{"id":377,"date":"2025-06-04T11:22:26","date_gmt":"2025-06-05T03:27:25","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=377"},"modified":"2025-06-05T11:22:26","modified_gmt":"2025-06-05T03:27:25","slug":"%e6%95%99%e5%ad%b8-swift-%e6%89%be%e5%ad%97%e4%b8%b2-%e6%96%87%e5%ad%97%e6%89%be%e5%ad%97%e4%b8%b2","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/%e6%95%99%e5%ad%b8-swift-%e6%89%be%e5%ad%97%e4%b8%b2-%e6%96%87%e5%ad%97%e6%89%be%e5%ad%97%e4%b8%b2\/","title":{"rendered":"\u30102025 \u6700\u65b0\u6559\u5b78\u3011Swift \u5b57\u4e32\u8207\u9663\u5217\u7684\u67e5\u8a62\u8207\u8655\u7406\u6280\u5de7"},"content":{"rendered":"<p>&#8220;`markdown<br \/>\n## \u4f7f\u7528 `contains` \u65b9\u6cd5\u67e5\u627e\u5b57\u4e32<\/p>\n<p>\u5728 Swift \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 `contains` \u65b9\u6cd5\u4f86\u6aa2\u67e5\u5b57\u4e32\u662f\u5426\u5305\u542b\u7279\u5b9a\u5b50\u5b57\u4e32\u3002\u4ee5\u4e0b\u662f 2025 \u5e74\u6700\u65b0\u7684\u8a9e\u6cd5\u793a\u7bc4\uff1a<\/p>\n<p>&#8220;`swift<br \/>\nlet str = &#8220;abcd1234&#8221;<br \/>\nprint(str.contains(&#8220;a&#8221;)) \/\/ true<br \/>\nprint(str.contains(&#8220;123&#8221;)) \/\/ true<br \/>\nprint(str.contains(&#8220;5678&#8221;)) \/\/ false<br \/>\n&#8220;`<\/p>\n<p>\u9019\u500b\u65b9\u6cd5\u4e0d\u50c5\u9069\u7528\u65bc\u5b57\u4e32\uff0c\u9084\u53ef\u4ee5\u7528\u65bc\u67e5\u627e\u9663\u5217\u4e2d\u7684\u91cd\u8907\u5b57\u4e32\u3002<\/p>\n<p>&#8220;`swift<br \/>\nlet nameArray = [&#8220;ab&#8221;, &#8220;cd&#8221;, &#8220;1234&#8221;]\nprint(nameArray.contains(&#8220;ab&#8221;)) \/\/ true<br \/>\nprint(nameArray.contains(&#8220;5678&#8221;)) \/\/ false<br \/>\n&#8220;`<\/p>\n<p>## \u4f7f\u7528 `Set` \u4f86\u8655\u7406\u91cd\u8907\u5143\u7d20<\/p>\n<p>\u5728 Swift \u4e2d\uff0c`Set` \u662f\u4e00\u7a2e\u7121\u5e8f\u7684\u96c6\u5408\uff0c\u53ef\u4ee5\u81ea\u52d5\u53bb\u9664\u91cd\u8907\u7684\u5143\u7d20\u3002\u9019\u88e1\u662f\u5982\u4f55\u4f7f\u7528 `Set` \u7684\u793a\u7bc4\uff1a<\/p>\n<p>&#8220;`swift<br \/>\nvar array: [String] = []\narray.append(&#8220;123&#8221;)<br \/>\narray.append(&#8220;456&#8221;)<br \/>\narray.append(&#8220;123&#8221;) \/\/ \u91cd\u8907\u503c<\/p>\n<p>var uniqueSet = Set<String>()<br \/>\nfor item in array {<br \/>\n    uniqueSet.insert(item) \/\/ \u53ea\u6703\u63d2\u5165\u552f\u4e00\u7684\u5143\u7d20<br \/>\n}<br \/>\n&#8220;`<\/p>\n<p>\u8f38\u51fa\u7d50\u679c\u5c07\u662f\uff1a<\/p>\n<p>&#8220;`swift<br \/>\nprint(uniqueSet) \/\/ \u8f38\u51fa: [&#8220;123&#8221;, &#8220;456&#8221;]\n&#8220;`<\/p>\n<p>## \u6aa2\u67e5\u5169\u500b\u9663\u5217\u7684\u5171\u540c\u5143\u7d20<\/p>\n<p>\u4f60\u53ef\u80fd\u9700\u8981\u53bb\u6aa2\u67e5\u5169\u500b\u9663\u5217\u4e2d\u6709\u54ea\u4e9b\u5143\u7d20\u662f\u5171\u540c\u7684\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u51fd\u6578\u793a\u7bc4\uff0c\u8a72\u51fd\u6578\u6703\u8fd4\u56de\u5169\u500b\u9663\u5217\u4e2d\u76f8\u540c\u7684\u5143\u7d20\uff1a<\/p>\n<p>&#8220;`swift<br \/>\nfunc checkArray(array1: [String], array2: [String]) -> [String] {<br \/>\n    var commonElements = [String]()<br \/>\n    var set = Set<String>()<\/p>\n<p>    for item in array1 {<br \/>\n        set.insert(item) \/\/ \u5c07\u7b2c\u4e00\u500b\u9663\u5217\u7684\u5143\u7d20\u52a0\u5165 Set<br \/>\n    }<\/p>\n<p>    for item in array2 {<br \/>\n        if set.contains(item) {<br \/>\n            commonElements.append(item) \/\/ \u5982\u679c\u5b58\u5728\u65bc Set \u4e2d\uff0c\u5247\u52a0\u5165\u5171\u540c\u5143\u7d20<br \/>\n        }<br \/>\n    }<br \/>\n    return commonElements<br \/>\n}<br \/>\n&#8220;`<\/p>\n<p>\u4f7f\u7528\u6b64\u51fd\u6578\uff1a<\/p>\n<p>&#8220;`swift<br \/>\nlet array1 = [&#8220;123&#8221;, &#8220;123&#8221;, &#8220;456&#8221;, &#8220;789&#8221;, &#8220;012&#8221;]\nlet array2 = [&#8220;123&#8221;, &#8220;456&#8221;, &#8220;1&#8221;, &#8220;2&#8221;]\nlet result = checkArray(array1: array1, array2: array2)<br \/>\nprint(result) \/\/ \u8f38\u51fa: [&#8220;123&#8221;, &#8220;456&#8221;]\n&#8220;`<\/p>\n<p>## \u932f\u8aa4\u6392\u9664<\/p>\n<p>\u5728\u4f7f\u7528\u4e0a\u8ff0\u8a9e\u6cd5\u6642\uff0c\u5e38\u898b\u7684\u932f\u8aa4\u53ef\u80fd\u5305\u62ec\uff1a<\/p>\n<p>1. **\u6578\u64da\u985e\u578b\u4e0d\u5339\u914d**\uff1a\u78ba\u4fdd\u6240\u6709\u6bd4\u8f03\u7684\u503c\u90fd\u662f\u540c\u4e00\u985e\u578b\uff08\u4f8b\u5982\uff0c\u90fd\u662f\u5b57\u4e32\uff09\u3002<br \/>\n2. **\u4f7f\u7528\u4e0d\u7576\u7684\u51fd\u6578**\uff1a\u78ba\u8a8d\u6240\u7528\u7684\u51fd\u6578\u5982 `contains` \u548c `insert` \u7684\u4f7f\u7528\u65b9\u5f0f\u6b63\u78ba\u3002<\/p>\n<p>## \u5ef6\u4f38\u61c9\u7528<\/p>\n<p>\u9019\u4e9b\u67e5\u8a62\u548c\u8655\u7406\u6280\u8853\u53ef\u4ee5\u7528\u4f86\u958b\u767c\u66f4\u8907\u96dc\u7684\u529f\u80fd\uff0c\u4f8b\u5982\uff1a<\/p>\n<p>&#8211; \u6aa2\u67e5\u7528\u6236\u8f38\u5165\u7684\u6709\u6548\u6027\u3002<br \/>\n&#8211; \u5728\u61c9\u7528\u7a0b\u5f0f\u4e2d\u67e5\u627e\u91cd\u8907\u9805\u76ee\u3002<br \/>\n&#8211; \u6574\u5408\u6578\u64da\u4ee5\u88fd\u4f5c\u5831\u544a\u3002<\/p>\n<p>![Swift\u7a0b\u5f0f\u6559\u5b78\u5716\u7247](https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2020\/11\/cup-1-1.png)<br \/>\n*\u5716\u7247\u4f86\u6e90\uff1aSwift\u7a0b\u5f0f\u6559\u5b78\u793a\u7bc4*<br \/>\n&#8220;`<\/p>\n<p>### Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/p>\n<p>**Q1: Swift \u4e2d\u7684 `Set` \u8207\u9663\u5217\u6709\u4ec0\u9ebc\u4e0d\u540c\uff1f**<br \/>\nA1: `Set` \u662f\u4e00\u500b\u7121\u5e8f\u7684\u96c6\u5408\uff0c\u4e26\u4e14\u4e0d\u5141\u8a31\u91cd\u8907\u5143\u7d20\uff0c\u800c\u9663\u5217\u662f\u6709\u5e8f\u7684\u4e14\u53ef\u4ee5\u5305\u542b\u91cd\u8907\u5143\u7d20\u3002<\/p>\n<p>**Q2: \u5982\u4f55\u6aa2\u67e5\u4e00\u500b\u5b57\u4e32\u662f\u5426\u5305\u542b\u591a\u500b\u5b50\u5b57\u4e32\uff1f**<br \/>\nA2: \u53ef\u4ee5\u4f7f\u7528 `contains` \u65b9\u6cd5\u9032\u884c\u591a\u6b21\u6aa2\u67e5\uff0c\u6216\u8005\u5c07\u6240\u6709\u5b50\u5b57\u4e32\u653e\u5165\u4e00\u500b\u9663\u5217\u4e2d\u4e26\u4f7f\u7528\u5faa\u74b0\u4f86\u9032\u884c\u6aa2\u67e5\u3002<\/p>\n<p>**Q3: \u4f7f\u7528 `Set` \u4f86\u53bb\u9664\u91cd\u8907\u5143\u7d20\u7684\u6548\u7387\u5982\u4f55\uff1f**<br \/>\nA3: \u4f7f\u7528 `Set` \u7684\u6548\u7387\u901a\u5e38\u6bd4\u4f7f\u7528\u9663\u5217\u4f86\u624b\u52d5\u6aa2\u67e5\u91cd\u8907\u5143\u7d20\u8981\u9ad8\uff0c\u56e0\u70ba `Set` \u662f\u57fa\u65bc\u54c8\u5e0c\u8868\u5be6\u73fe\u7684\uff0c\u67e5\u627e\u901f\u5ea6\u8f03\u5feb\u3002<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;`markdown ## \u4f7f\u7528 `c&#8230;<\/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":[],"class_list":["post-377","post","type-post","status-publish","format-aside","has-post-thumbnail","hentry","category-swift","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-65","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/377","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=377"}],"version-history":[{"count":5,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/377\/revisions"}],"predecessor-version":[{"id":7566,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/377\/revisions\/7566"}],"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=377"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=377"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=377"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}