{"id":343,"date":"2025-06-03T10:24:36","date_gmt":"2025-06-05T03:20:35","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=343"},"modified":"2025-06-05T10:24:36","modified_gmt":"2025-06-05T03:20:35","slug":"swift-%e5%ad%97%e4%b8%b2%e6%93%b7%e5%8f%96-%e6%96%87%e5%ad%97%e6%93%b7%e5%8f%96","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/swift-%e5%ad%97%e4%b8%b2%e6%93%b7%e5%8f%96-%e6%96%87%e5%ad%97%e6%93%b7%e5%8f%96\/","title":{"rendered":"**Swift \u5b57\u4e32\u64f7\u53d6\uff1a2025 \u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10**"},"content":{"rendered":"<p>&#8220;`html<\/p>\n<h3>\u5b57\u4e32\u64f7\u53d6<\/h3>\n<p>\u5728 Swift \u4e2d\uff0c\u5b57\u4e32\u64f7\u53d6\u662f\u975e\u5e38\u5e38\u7528\u7684\u64cd\u4f5c\u3002\u900f\u904e\u4ee5\u4e0b\u65b9\u6cd5\uff0c\u4f60\u53ef\u4ee5\u65b9\u4fbf\u5730\u64f7\u53d6\u5b57\u4e32\u7684\u7279\u5b9a\u90e8\u5206\uff0c\u9019\u4e9b\u65b9\u6cd5\u5c07\u6703\u4f7f\u7528\u5230 Swift 5.7 \u7684\u6700\u65b0\u8a9e\u6cd5\u3002<\/p>\n<h4>\u4f7f\u7528 index \u548c offsetBy \u9032\u884c\u5b57\u4e32\u64f7\u53d6<\/h4>\n<p>\u4f60\u53ef\u4ee5\u5229\u7528 <code>index(_:offsetBy:)<\/code> \u65b9\u6cd5\u4f86\u7372\u5f97\u5b57\u4e32\u7684\u7279\u5b9a\u4f4d\u7f6e\uff0c\u4e26\u6839\u64da\u9019\u4e9b\u4f4d\u7f6e\u64f7\u53d6\u5b57\u4e32\u7684\u524d\u7db4\u6216\u5f8c\u7db4\u3002<\/p>\n<pre><code class=\"language-swift line-numbers\">let cutString = \"hello world\"\nlet helloIndex = cutString.index(cutString.startIndex, offsetBy: 5)\nprint(String(cutString.suffix(from: helloIndex)))  \n\/\/ \u8f38\u51fa: \" world\"\nprint(String(cutString.prefix(upTo: helloIndex))) \n\/\/ \u8f38\u51fa: \"hello\"\n\n\/\/ \u5f9e\u5f8c\u9762\u6578\u4f86 \u7b2c-5\u500b\nlet worldIndex = cutString.index(cutString.endIndex, offsetBy: -5)\nprint(String(cutString.suffix(from: worldIndex))) \n\/\/ \u8f38\u51fa: \"world\"\nprint(String(cutString.prefix(upTo: worldIndex)))  \n\/\/ \u8f38\u51fa: \"hello \"\n<\/code><\/pre>\n<h4>\u4f7f\u7528 index(before:) \u53d6\u5f97\u4e4b\u524d\u7684\u5b57\u4e32\u90e8\u5206<\/h4>\n<pre><code class=\"language-swift line-numbers\">\/\/ \u5728 index \u4e4b\u524d\nprint(String(cutString.suffix(from: cutString.index(before: cutString.endIndex))))  \n\/\/ \u8f38\u51fa: \"ello world\"\nprint(String(cutString.prefix(upTo: cutString.index(before: cutString.endIndex))))  \n\/\/ \u8f38\u51fa: \"h\"\n<\/code><\/pre>\n<h4>\u4f7f\u7528 index(after:) \u53d6\u5f97\u4e4b\u5f8c\u7684\u5b57\u4e32\u90e8\u5206<\/h4>\n<pre><code class=\"language-swift line-numbers\">\/\/ \u5728 index \u4e4b\u5f8c\nprint(String(cutString.suffix(from: cutString.index(after: cutString.startIndex))))  \n\/\/ \u8f38\u51fa: \"ello world\"\nprint(String(cutString.prefix(upTo: cutString.index(after: cutString.startIndex))))  \n\/\/ \u8f38\u51fa: \"h\"\n<\/code><\/pre>\n<h4>\u76f4\u63a5\u4f7f\u7528 index \u985e\u578b\u4f86\u64f7\u53d6\u5b57\u4e32<\/h4>\n<pre><code class=\"language-swift line-numbers\">print(String(cutString.prefix(5)))  \n\/\/ \u8f38\u51fa: \"hello\"\nprint(String(cutString[..<helloIndex])) \n\/\/ \u8f38\u51fa: \"hello\"\n<\/code><\/pre>\n<h4>\u53d6\u7bc4\u570d\u5b57\u4e32\uff1a\u64f7\u53d6\u7b2c2~3\u500b\u5b57<\/h4>\n<p>\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528 <code>NSRange<\/code> \u4f86\u64f7\u53d6\u6307\u5b9a\u7bc4\u570d\u7684\u5b57\u4e32\u3002\u4ee5\u4e0b\u662f\u4e00\u500b\u7bc4\u4f8b\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let cutBugString = \"hello world\"\nprint((cutBugString as NSString).substring(with: NSMakeRange(2, 3)))\n\/\/ \u8f38\u51fa: \"llo\"\n<\/code><\/pre>\n<h3>\u5e38\u898b\u554f\u984c\u8207\u89e3\u7b54\uff08Q&A\uff09<\/h3>\n<h4>1. \u5982\u4f55\u5728 Swift \u4e2d\u6aa2\u67e5\u5b57\u4e32\u662f\u5426\u5305\u542b\u7279\u5b9a\u5b57\u4e32\uff1f<\/h4>\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>contains<\/code> \u65b9\u6cd5\u4f86\u6aa2\u67e5\u5b57\u4e32\u662f\u5426\u5305\u542b\u53e6\u4e00\u500b\u5b57\u4e32\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let text = \"hello world\"\nlet containsHello = text.contains(\"hello\") \/\/ \u8fd4\u56de true\n<\/code><\/pre>\n<h4>2. Swift \u4e2d\u7684\u5b57\u4e32\u662f\u503c\u985e\u578b\u9084\u662f\u5f15\u7528\u985e\u578b\uff1f<\/h4>\n<p>\u5728 Swift \u4e2d\uff0c\u5b57\u4e32\u662f\u503c\u985e\u578b\uff0c\u9019\u610f\u5473\u8457\u7576\u4f60\u5c07\u5b57\u4e32\u8ce6\u503c\u7d66\u53e6\u4e00\u500b\u8b8a\u6578\u6642\uff0c\u6703\u5275\u5efa\u4e00\u500b\u7368\u7acb\u7684\u526f\u672c\u3002<\/p>\n<h4>3. \u5982\u4f55\u5feb\u901f\u53cd\u8f49\u4e00\u500b\u5b57\u4e32\uff1f<\/h4>\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>reversed()<\/code> \u65b9\u6cd5\u4f86\u53cd\u8f49\u5b57\u4e32\uff0c\u4e26\u5229\u7528 <code>String<\/code> \u7684\u521d\u59cb\u5316\u65b9\u6cd5\u8f49\u63db\u56de\u5b57\u4e32\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let originalString = \"hello\"\nlet reversedString = String(originalString.reversed()) \/\/ \"olleh\"\n<\/code><\/pre>\n<div style=\"font-family: 'times new roman'; text-align: left;\">\n    <span style=\"font-size: x-large;\"><b style=\"background-color: white;\">Swift\u66f4\u591a\u6587\u7ae0<\/b><\/span>\n<\/div>\n<p><span style=\"font-family: times new roman; font-size: large;\"><br \/>\n    <a href=\"https:\/\/badgameshow.com\/steven\/swift\/\u6559\u5b78-swift-\u627e\u5b57\u4e32-\u6587\u5b57\u627e\u5b57\u4e32\/\" target=\"_blank\" rel=\"noopener\">[\u6559\u5b78] Swift \u627e\u5b57\u4e32 \u6587\u5b57\u627e\u5b57\u4e32<\/a><br \/>\n    <a href=\"https:\/\/badgameshow.com\/steven\/swift\/swift-\u5b57\u4e32\u62fc\u63a5-\u6587\u5b57\u62fc\u63a5\/\" target=\"_blank\" rel=\"noopener\">Swift\u5b57\u4e32\u62fc\u63a5 \u6587\u5b57\u62fc\u63a5<\/a><br \/>\n    <a href=\"https:\/\/badgameshow.com\/steven\/swift\/swift-\u5b57\u4e32\u64f7\u53d6-\u6587\u5b57\u64f7\u53d6\/\" target=\"_blank\" rel=\"noopener\">Swift \u5b57\u4e32\u64f7\u53d6 \u6587\u5b57\u64f7\u53d6<\/a><br \/>\n    <a href=\"https:\/\/badgameshow.com\/steven\/swift\/swift-\u9663\u5217\u8f49\u5b57\u4e32-array-to-string-description\/\" target=\"_blank\" rel=\"noopener\">Swift - \u9663\u5217\u8f49\u5b57\u4e32 | Array to String | List to String | description<\/a><br \/>\n    <a href=\"https:\/\/badgameshow.com\/steven\/swift\/swift-date-\u73fe\u5728\u661f\u671f\u5e7e-\u9019\u500b\u6708\u6709\u5e7e\u5929\/\" target=\"_blank\" rel=\"noopener\">Swift Date \u73fe\u5728\u661f\u671f\u5e7e \u9019\u500b\u6708\u6709\u5e7e\u5929<\/a><br \/>\n    <a href=\"https:\/\/badgameshow.com\/steven\/swift\/swift-\u6b63\u898f\u8868\u9054\u5f0f-\u96fb\u8a71-\u8eab\u5206\u8b49-email\/\" target=\"_blank\" rel=\"noopener\">Swift - \u6b63\u898f\u8868\u9054\u5f0f (\u96fb\u8a71\/\u8eab\u5206\u8b49\/email)<\/a><br \/>\n<\/span><br \/>\n```<br \/>\n---<\/p>\n","protected":false},"excerpt":{"rendered":"<p>let helloIndex = cutString.index(cutString.startIndex, offsetBy: 5)<br \/>\nprint(String(cutString.suffix(from: helloIndex)))<br \/>\n\/\/ -> &#8221; world&#8221;<br \/>\nprint(String(cutString.prefix(upTo: helloIndex)))<br \/>\n\/\/ -> &#8220;hello&#8221;<\/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-343","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-5x","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/343","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=343"}],"version-history":[{"count":6,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/343\/revisions"}],"predecessor-version":[{"id":7552,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/343\/revisions\/7552"}],"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=343"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=343"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=343"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}