{"id":6686,"date":"2025-06-02T11:50:44","date_gmt":"2025-06-05T04:50:43","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=6686"},"modified":"2025-06-05T11:50:44","modified_gmt":"2025-06-05T04:50:43","slug":"https-badgameshow-com-steven-21","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-21\/","title":{"rendered":"Swift 2025 \u6700\u65b0\u7248\u8a08\u6642\u5668\u5be6\u4f5c\u6559\u5b78 \u23f0"},"content":{"rendered":"<p>&#8220;`html<br \/>\n<meta name=\"keywords\" content=\"swift, \u8a08\u6642\u5668, \u6700\u65b0\u65b9\u6cd5, iOS \u958b\u767c, Timer\"><\/p>\n<h1>Swift 2025 \u6700\u65b0\u7248\u8a08\u6642\u5668\u5be6\u4f5c\u6559\u5b78 \u23f0<\/h1>\n<p>\u5728\u958b\u767c iOS App \u6642\uff0c\u8a08\u6642\u5668\u662f\u4e00\u500b\u5e38\u7528\u7684\u529f\u80fd\uff0c\u7121\u8ad6\u662f\u7528\u65bc\u5012\u6578\u8a08\u6642\u3001\u6bcf\u79d2\u66f4\u65b0\u6642\u9593\uff0c\u6216\u662f\u57f7\u884c\u5b9a\u6642\u4efb\u52d9\u3002\u5728 Swift \u4e2d\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528 `Timer` \u985e\u4f86\u5be6\u73fe\u9019\u4e9b\u529f\u80fd\u3002\u672c\u6587\u5c07\u63d0\u4f9b 2025 \u5e74\u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10\uff0c\u5e6b\u52a9\u60a8\u6709\u6548\u904b\u7528\u8a08\u6642\u5668\u3002<\/p>\n<h2>\u4f7f\u7528 Timer \u5be6\u73fe\u5012\u6578\u8a08\u6642<\/h2>\n<p>\u8981\u4f7f\u7528 `Timer` \u5be6\u73fe\u5012\u6578\u8a08\u6642\uff0c\u6211\u5011\u53ef\u4ee5\u900f\u904e `scheduledTimer` \u65b9\u6cd5\u4f86\u8a2d\u5b9a\u8a08\u6642\u5668\u3002\u4ee5\u4e0b\u662f\u5b8c\u6574\u7684\u5be6\u4f5c\u7bc4\u4f8b\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">\/\/ \u5ba3\u544a\u4e00\u500b Timer\nvar timer: Timer?\n\n\/\/ \u8a08\u6578\u8b8a\u6578\u521d\u59cb\u503c\nvar count = 10\n\n\/\/ \u5ba3\u544a\u4e00\u500b\u65b9\u6cd5\uff0c\u6bcf\u6b21\u9593\u9694\u6642\u9593\u5230\u4e86\uff0c\u5c31\u6703\u57f7\u884c\u9019\u500b\u65b9\u6cd5\n@objc func updateTime() {\n    \/\/ \u6bcf\u6b21\u57f7\u884c\u6642\uff0c\u5c07\u8a08\u6578\u8b8a\u6578\u6e1b 1\n    count -= 1\n    \/\/ \u5982\u679c\u8a08\u6578\u8b8a\u6578\u5c0f\u65bc\u7b49\u65bc 0\uff0c\u5247\u505c\u6b62 Timer\n    if count <= 0 {\n        timer?.invalidate()\n        label.text = \"\u6642\u9593\u5230\uff01\"\n    } else {\n        \/\/ \u5c07\u8a08\u6578\u8b8a\u6578\u986f\u793a\u5728 Label \u4e0a\n        label.text = \"\\(count) \u79d2\"\n    }\n}\n\n\/\/ \u555f\u52d5 Timer\uff0c\u6bcf\u79d2\u57f7\u884c\u4e00\u6b21 updateTime \u65b9\u6cd5\ntimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateTime), userInfo: nil, repeats: true)\n<\/code><\/pre>\n<h2>\u4f7f\u7528 Timer \u5be6\u73fe\u6bcf\u79d2\u66f4\u65b0\u6642\u9593<\/h2>\n<p>\u9664\u4e86\u5012\u6578\u8a08\u6642\uff0c\u6211\u5011\u4e5f\u53ef\u4ee5\u4f7f\u7528 `Timer` \u4f86\u5be6\u73fe\u6bcf\u79d2\u66f4\u65b0\u7684\u6642\u9418\u529f\u80fd\u3002\u4ee5\u4e0b\u662f\u7bc4\u4f8b\u7a0b\u5f0f\u78bc\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">\/\/ \u5ba3\u544a\u4e00\u500b Timer\nvar timer: Timer?\n\n\/\/ \u5ba3\u544a\u4e00\u500b\u65b9\u6cd5\uff0c\u6bcf\u6b21\u9593\u9694\u6642\u9593\u5230\u4e86\uff0c\u5c31\u6703\u57f7\u884c\u9019\u500b\u65b9\u6cd5\n@objc func updateCurrentTime() {\n    \/\/ \u53d6\u5f97\u76ee\u524d\u6642\u9593\n    let date = Date()\n    \/\/ \u5c07\u6642\u9593\u683c\u5f0f\u5316\u70ba\u5b57\u4e32\n    let dateFormatter = DateFormatter()\n    dateFormatter.dateFormat = \"HH:mm:ss\"\n    let dateString = dateFormatter.string(from: date)\n    \/\/ \u5c07\u6642\u9593\u986f\u793a\u5728 Label \u4e0a\n    label.text = dateString\n}\n\n\/\/ \u555f\u52d5 Timer\uff0c\u6bcf\u79d2\u57f7\u884c\u4e00\u6b21 updateCurrentTime \u65b9\u6cd5\ntimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateCurrentTime), userInfo: nil, repeats: true)\n<\/code><\/pre>\n<h2>\u932f\u8aa4\u6392\u9664\u8207\u6700\u4f73\u5be6\u8e10<\/h2>\n<p>\u5728\u4f7f\u7528 `Timer` \u6642\uff0c\u6709\u5e7e\u9ede\u9700\u8981\u7279\u5225\u6ce8\u610f\uff1a<\/p>\n<p>1. **\u78ba\u4fdd Timer \u6703\u88ab\u6b63\u78ba\u5730\u5931\u6548**\uff1a\u5982\u679c\u4e0d\u6b63\u78ba\u505c\u6b62 Timer\uff0c\u5c07\u53ef\u80fd\u5c0e\u81f4\u8a18\u61b6\u9ad4\u6d29\u6f0f\u6216\u61c9\u7528\u5d29\u6f70\u3002\u4f7f\u7528 `invalidate()` \u65b9\u6cd5\u4f86\u505c\u6b62 Timer\u3002<br \/>\n2. **\u4f7f\u7528\u4e3b\u57f7\u884c\u7dd2\uff08Main Thread\uff09**\uff1a\u78ba\u4fdd\u8a08\u6642\u5668\u7684\u56de\u8abf\u65b9\u6cd5\u5728\u4e3b\u57f7\u884c\u7dd2\u4e2d\u57f7\u884c\uff0c\u9019\u6a23\u53ef\u4ee5\u907f\u514d\u66f4\u65b0 UI \u6642\u51fa\u73fe\u7570\u5e38\u3002<\/p>\n<h2>\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u8a08\u6642\u5668\u4e0d\u50c5\u53ef\u4ee5\u7528\u65bc\u7c21\u55ae\u7684\u5012\u6578\u8a08\u6642\u6216\u986f\u793a\u7576\u524d\u6642\u9593\uff0c\u9084\u53ef\u4ee5\u61c9\u7528\u65bc\u904a\u6232\u8a08\u6642\u3001\u5b9a\u6642\u4efb\u52d9\u57f7\u884c\u7b49\u591a\u7a2e\u5834\u666f\u3002\u60a8\u53ef\u4ee5\u6839\u64da\u9700\u6c42\u8abf\u6574\u8a08\u6642\u5668\u7684\u6642\u9593\u9593\u9694\u53ca\u57f7\u884c\u7684\u65b9\u5f0f\uff0c\u589e\u52a0\u61c9\u7528\u7684\u4e92\u52d5\u6027\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u7c21\u55ae\u7684\u8a08\u6642\u5668\u65b9\u6cd5-\u23f0_1675393844.210444.webp\" alt=\"Swift \u7c21\u55ae\u7684\u8a08\u6642\u5668\u65b9\u6cd5 \u23f0\" title=\"Swift \u8a08\u6642\u5668, Swift Timer \u65b9\u6cd5, Swift \u5feb\u901f\u5efa\u7acb\u8a08\u6642\u5668, Swift \u8a08\u6642\u5668\u529f\u80fd, Swift \u8a08\u6642\u5668\u61c9\u7528\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>Q&A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>1. Timer \u5982\u4f55\u5728\u80cc\u666f\u57f7\u884c\uff1f<\/h3>\n<p>\u7b54\uff1a\u5728 iOS \u4e2d\uff0cTimer \u5728\u61c9\u7528\u9032\u5165\u80cc\u666f\u5f8c\u6703\u81ea\u52d5\u505c\u6b62\u3002\u5982\u679c\u9700\u8981\u5728\u80cc\u666f\u57f7\u884c\u5b9a\u6642\u4efb\u52d9\uff0c\u5efa\u8b70\u4f7f\u7528 `DispatchSourceTimer`\u3002<\/p>\n<h3>2. Timer \u548c CADisplayLink \u7684\u5340\u5225\u662f\u4ec0\u9ebc\uff1f<\/h3>\n<p>\u7b54\uff1aTimer \u7528\u65bc\u5b9a\u6642\u57f7\u884c\u67d0\u500b\u51fd\u6578\uff0c\u800c CADisplayLink \u662f\u7528\u65bc\u8207\u5c4f\u5e55\u5237\u65b0\u540c\u6b65\u7684\u8a08\u6642\u5668\uff0c\u901a\u5e38\u7528\u65bc\u52d5\u756b\u6216\u904a\u6232\u958b\u767c\u3002<\/p>\n<h3>3. \u5982\u4f55\u63a7\u5236 Timer \u7684\u7cbe\u78ba\u5ea6\uff1f<\/h3>\n<p>\u7b54\uff1aTimer \u7684\u7cbe\u78ba\u5ea6\u53d7\u9650\u65bc\u7cfb\u7d71\u8ca0\u8f09\u548c\u4e3b\u57f7\u884c\u7dd2\u7684\u7e41\u5fd9\u7a0b\u5ea6\u3002\u5982\u679c\u9700\u8981\u66f4\u9ad8\u7cbe\u78ba\u5ea6\u7684\u8a08\u6642\uff0c\u53ef\u4ee5\u8003\u616e\u4f7f\u7528 `DispatchSourceTimer` \u6216 `CADisplayLink`\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f Swift \u4e2d 2025 \u5e74\u6700\u65b0\u7684\u8a08\u6642\u5668\u5be6\u4f5c\u65b9\u6cd5\uff0c\u5e0c\u671b\u80fd\u5e6b\u52a9\u60a8\u5728 iOS \u958b\u767c\u4e2d\u66f4\u6709\u6548\u5730\u4f7f\u7528\u8a08\u6642\u5668\u529f\u80fd\u3002<br \/>\n```<br \/>\n---<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528Swift\u4f86\u5efa\u7acb\u8a08\u6642\u5668\uff0c\u4e86\u89e3Swift\u4e2d\u7684Timer\u65b9\u6cd5\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83\u4f86\u5efa\u7acb\u8a08\u6642\u5668\uff0c\u8b93\u4f60\u53ef\u4ee5\u66f4\u6709\u6548\u7387\u7684\u5b8c\u6210\u5de5\u4f5c\u3002<\/p>\n","protected":false},"author":1,"featured_media":6685,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[174,5],"tags":[173,9],"class_list":["post-6686","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ios","category-swift","tag-ios","tag-swift"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u7c21\u55ae\u7684\u8a08\u6642\u5668\u65b9\u6cd5-\u23f0_1675393844.210444.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1JQ","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6686","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=6686"}],"version-history":[{"count":0,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6686\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/6685"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=6686"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=6686"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=6686"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}