{"id":371,"date":"2025-05-30T11:00:26","date_gmt":"2025-06-05T03:23:26","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=371"},"modified":"2025-06-05T11:00:26","modified_gmt":"2025-06-05T03:23:26","slug":"%e6%95%99%e5%ad%b8-swift-%e5%96%ae%e7%b7%9a%e7%a8%8b-%e5%a4%9a%e7%b7%9a%e7%a8%8b-%e7%af%84%e4%be%8b","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/%e6%95%99%e5%ad%b8-swift-%e5%96%ae%e7%b7%9a%e7%a8%8b-%e5%a4%9a%e7%b7%9a%e7%a8%8b-%e7%af%84%e4%be%8b\/","title":{"rendered":"\u30102025 \u6700\u65b0\u3011Swift \u55ae\u7dda\u7a0b\u8207\u591a\u7dda\u7a0b\u7684 GCD \u4f7f\u7528\u6559\u5b78"},"content":{"rendered":"<p>&#8220;`html<\/p>\n<h1>\u30102025 \u6700\u65b0\u3011Swift \u55ae\u7dda\u7a0b\u8207\u591a\u7dda\u7a0b\u7684 GCD \u4f7f\u7528\u6559\u5b78<\/h1>\n<h2>\u4ec0\u9ebc\u662f Grand Central Dispatch (GCD)\uff1f<\/h2>\n<p>Grand Central Dispatch\uff08GCD\uff09\u662f Apple \u63d0\u4f9b\u7684\u4e00\u500b\u5f37\u5927\u7684\u591a\u7dda\u7a0b\u8655\u7406\u6a5f\u5236\uff0c\u80fd\u5920\u7c21\u5316\u591a\u7dda\u7a0b\u7de8\u7a0b\u7684\u8907\u96dc\u6027\u3002\u5b83\u4f7f\u5f97\u958b\u767c\u8005\u53ef\u4ee5\u66f4\u6709\u6548\u5730\u7ba1\u7406\u57f7\u884c\u7dd2\uff0c\u5f9e\u800c\u63d0\u5347\u61c9\u7528\u7a0b\u5f0f\u7684\u6027\u80fd\u548c\u97ff\u61c9\u901f\u5ea6\u3002<\/p>\n<h2>\u975e\u540c\u6b65\u8207\u540c\u6b65\u7684\u57fa\u672c\u6982\u5ff5<\/h2>\n<p>\u5728 GCD \u4e2d\uff0c\u6211\u5011\u7d93\u5e38\u6703\u9047\u5230\u975e\u540c\u6b65\uff08async\uff09\u8207\u540c\u6b65\uff08sync\uff09\u7684\u64cd\u4f5c\uff1a<\/p>\n<p>&#8211; **\u975e\u540c\u6b65 (async)**\uff1a\u4efb\u52d9\u6703\u7acb\u5373\u8fd4\u56de\uff0c\u4e0d\u6703\u963b\u585e\u7576\u524d\u57f7\u884c\u7dd2\uff0c\u9069\u5408\u57f7\u884c\u9700\u8981\u8f03\u9577\u6642\u9593\u7684\u4efb\u52d9\u3002<br \/>\n&#8211; **\u540c\u6b65 (sync)**\uff1a\u4efb\u52d9\u6703\u6309\u7167\u9806\u5e8f\u57f7\u884c\uff0c\u7576\u524d\u57f7\u884c\u7dd2\u6703\u88ab\u963b\u585e\uff0c\u76f4\u5230\u4efb\u52d9\u5b8c\u6210\u3002<\/p>\n<p>\u8209\u500b\u4f8b\u5b50\uff0c\u53ef\u4ee5\u60f3\u8c61\u4e00\u7fa4\u5927\u5abd\u9032\u5ec1\u6240\u7684\u60c5\u666f\uff1a<br \/>\n&#8211; **\u975e\u540c\u6b65**\uff1a\u4e00\u7fa4\u5927\u5abd\u540c\u6642\u885d\u9032\u5ec1\u6240\u3002<br \/>\n&#8211; **\u540c\u6b65**\uff1a\u5927\u5abd\u5011\u6392\u968a\u4f9d\u6b21\u9032\u5165\u5ec1\u6240\u3002<\/p>\n<h2>GCD \u7684\u57fa\u672c\u4f7f\u7528\u65b9\u6cd5<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4f7f\u7528 GCD \u7684\u57fa\u672c\u7bc4\u4f8b\uff1a<\/p>\n<p>1. **\u975e\u540c\u6b65\u4efb\u52d9**\uff1a\u4f7f\u7528 `DispatchQueue.global` \u4f86\u57f7\u884c\u4efb\u52d9\u3002<\/p>\n<p>&#8220;`swift<br \/>\nvar testStr: String = &#8220;1&#8221;<br \/>\nDispatchQueue.global(qos: .userInitiated).async {<br \/>\n    testStr = &#8220;2&#8221;<br \/>\n}<br \/>\n&#8220;`<\/p>\n<p>\u5728\u9019\u6bb5\u7a0b\u5f0f\u78bc\u4e2d\uff0c`testStr` \u7684\u503c\u6703\u88ab\u66f4\u65b0\u70ba &#8220;2&#8221;\uff0c\u4f46\u9019\u662f\u5728\u80cc\u666f\u57f7\u884c\u7dd2\u4e2d\u9032\u884c\u7684\u3002<\/p>\n<p>2. **\u66f4\u65b0 UI \u7684\u4e3b\u57f7\u884c\u7dd2**\uff1a\u5982\u679c\u8981\u5728\u4e3b\u57f7\u884c\u7dd2\u66f4\u65b0 UI\uff0c\u5247\u9700\u8981\u4f7f\u7528 `DispatchQueue.main.async`\uff1a<\/p>\n<p>&#8220;`swift<br \/>\nDispatchQueue.main.async {<br \/>\n    self.dateLabel.text = testStr<br \/>\n}<br \/>\n&#8220;`<\/p>\n<p>3. **\u7d50\u5408\u975e\u540c\u6b65\u8207\u540c\u6b65**\uff1a\u5982\u679c\u9700\u8981\u5728\u66f4\u65b0\u5b8c\u8b8a\u6578\u5f8c\u518d\u66f4\u65b0 UI\uff0c\u53ef\u4ee5\u9019\u6a23\u5beb\uff1a<\/p>\n<p>&#8220;`swift<br \/>\nvar testStr: String = &#8220;1&#8221;<br \/>\nDispatchQueue.global(qos: .userInitiated).async {<br \/>\n    testStr = &#8220;2&#8221;<br \/>\n    DispatchQueue.main.async {<br \/>\n        self.dateLabel.text = testStr<br \/>\n    }<br \/>\n}<br \/>\n&#8220;`<\/p>\n<h2>\u5ef6\u9072\u57f7\u884c\u4efb\u52d9<\/h2>\n<p>\u5982\u679c\u60a8\u9700\u8981\u5ef6\u9072\u57f7\u884c\u67d0\u4e9b\u4efb\u52d9\uff0c\u53ef\u4ee5\u4f7f\u7528 `DispatchQueue.main.asyncAfter` \u65b9\u6cd5\uff1a<\/p>\n<p>&#8220;`swift<br \/>\nDispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {<br \/>\n    self.addView()<br \/>\n}<br \/>\n&#8220;`<\/p>\n<p>\u9019\u6bb5\u7a0b\u5f0f\u78bc\u6703\u5728 1 \u79d2\u5f8c\u57f7\u884c `addView()` \u65b9\u6cd5\u3002<\/p>\n<h2>Swift \u8207 Objective-C \u7684\u5c0d\u6bd4<\/h2>\n<p>\u4ee5\u4e0b\u662f\u5c07\u4e0a\u8ff0 Swift \u7a0b\u5f0f\u78bc\u8f49\u63db\u70ba Objective-C \u7684\u7bc4\u4f8b\uff1a<\/p>\n<p>&#8220;`objective-c<br \/>\nNSString *testStr = @&#8221;1&#8243;;<br \/>\ndispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{<br \/>\n    testStr = @&#8221;2&#8243;;<br \/>\n    dispatch_async(dispatch_get_main_queue(), ^{<br \/>\n        self.dateLabel.text = testStr;<br \/>\n    });<br \/>\n});<br \/>\n&#8220;`<\/p>\n<p>\u9019\u6a23\u7684\u5c0d\u6bd4\u6709\u52a9\u65bc\u4e86\u89e3 Swift \u8207 Objective-C \u5728\u591a\u7dda\u7a0b\u8655\u7406\u4e0a\u7684\u4e0d\u540c\u5beb\u6cd5\u3002<\/p>\n<h2>\u932f\u8aa4\u6392\u9664\u8207\u6700\u4f73\u5be6\u8e10<\/h2>\n<p>&#8211; \u78ba\u4fdd\u5728\u4e3b\u57f7\u884c\u7dd2\u4e2d\u66f4\u65b0 UI\uff0c\u5426\u5247\u53ef\u80fd\u6703\u5c0e\u81f4\u61c9\u7528\u7a0b\u5f0f\u5d29\u6f70\u3002<br \/>\n&#8211; \u4f7f\u7528\u9069\u7576\u7684 QoS\uff08\u670d\u52d9\u8cea\u91cf\uff09\u4f86\u512a\u5316\u4efb\u52d9\u7684\u57f7\u884c\u6548\u7387\u3002<br \/>\n&#8211; \u6e2c\u8a66\u591a\u7dda\u7a0b\u4ee3\u78bc\u4ee5\u78ba\u4fdd\u6c92\u6709\u7af6\u722d\u689d\u4ef6\u548c\u6b7b\u9396\u554f\u984c\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>GCD \u53ef\u4ee5\u7528\u65bc\u4ee5\u4e0b\u60c5\u5883\uff1a<br \/>\n&#8211; \u7db2\u7d61\u8acb\u6c42\u7684\u975e\u540c\u6b65\u8655\u7406<br \/>\n&#8211; \u5927\u6578\u64da\u8655\u7406\u6216\u8a08\u7b97<br \/>\n&#8211; \u7570\u6b65\u52a0\u8f09\u5716\u7247\u7b49\u8cc7\u6e90<\/p>\n<p><a class=\"wp-editor-md-post-content-link\" href=\"https:\/\/p.ecpay.com.tw\/99527\" target=\"_blank\" rel=\"noopener\"><br \/>\n    <img decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2020\/11\/cup-1-1.png\" alt=\"Swift GCD \u4f7f\u7528\u6559\u5b78\" title=\"\"><br \/>\n<\/a><\/p>\n<h2>\u5e38\u898b\u554f\u984c\u89e3\u7b54 (Q&#038;A)<\/h2>\n<h3>1. GCD \u8207 NSOperation \u6709\u4ec0\u9ebc\u5340\u5225\uff1f<\/h3>\n<p>GCD \u662f\u57fa\u65bc C \u8a9e\u8a00\u7684\u4f4e\u5c64\u6b21 API\uff0c\u63d0\u4f9b\u66f4\u9ad8\u6027\u80fd\u7684\u591a\u7dda\u7a0b\u8655\u7406\uff0c\u800c NSOperation \u63d0\u4f9b\u66f4\u9ad8\u5c64\u6b21\u7684\u62bd\u8c61\uff0c\u4fbf\u65bc\u7ba1\u7406\u548c\u8abf\u5ea6\u4efb\u52d9\u3002<\/p>\n<h3>2. \u5982\u4f55\u8655\u7406 GCD \u4e2d\u7684\u932f\u8aa4\uff1f<\/h3>\n<p>GCD \u672c\u8eab\u4e0d\u63d0\u4f9b\u932f\u8aa4\u8655\u7406\u6a5f\u5236\uff0c\u5efa\u8b70\u5728\u4efb\u52d9\u4e2d\u4f7f\u7528 Swift \u7684\u932f\u8aa4\u8655\u7406\u4f86\u6355\u7372\u548c\u8655\u7406\u53ef\u80fd\u7684\u932f\u8aa4\u3002<\/p>\n<h3>3. \u53ef\u4ee5\u5728 GCD \u4e2d\u4f7f\u7528 closure \u55ce\uff1f<\/h3>\n<p>\u662f\u7684\uff0cGCD \u652f\u6301\u4f7f\u7528 closure\uff0c\u9019\u4f7f\u5f97\u4ee3\u78bc\u66f4\u52a0\u7c21\u6f54\u6613\u8b80\u3002<\/p>\n<p>&#8220;`<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>var testStr : String = &#8220;1&#8221;<br \/>\nDispatchQueue.global(qos: .userInitiated).async {<br \/>\n    testStr = &#8220;2&#8221;<br \/>\n}<\/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-371","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-5Z","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/371","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=371"}],"version-history":[{"count":7,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/371\/revisions"}],"predecessor-version":[{"id":7584,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/371\/revisions\/7584"}],"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=371"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=371"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=371"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}