{"id":6838,"date":"2025-06-03T14:05:57","date_gmt":"2025-06-05T06:54:56","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=6838"},"modified":"2025-06-05T14:05:57","modified_gmt":"2025-06-05T06:54:56","slug":"https-badgameshow-com-steven-95","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-95\/","title":{"rendered":"\u638c\u63e1 Swift \u5b9a\u6642\u5668\uff1aNSTimer \u7684\u6700\u65b0\u4f7f\u7528\u6280\u5de7\u8207\u6700\u4f73\u5be6\u8e10 \ud83d\udd70\ufe0f"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"swift, NSTimer, Swift \u5b9a\u6642\u5668, iOS \u958b\u767c\"><\/p>\n<h1>\u638c\u63e1 Swift \u5b9a\u6642\u5668\uff1aNSTimer \u7684\u6700\u65b0\u4f7f\u7528\u6280\u5de7\u8207\u6700\u4f73\u5be6\u8e10 \ud83d\udd70\ufe0f<\/h1>\n<p>Swift \u662f\u4e00\u7a2e\u73fe\u4ee3\u5316\u7684\u7a0b\u5f0f\u8a9e\u8a00\uff0c\u63d0\u4f9b\u4e86\u591a\u7a2e\u5f37\u5927\u7684\u529f\u80fd\uff0c\u5176\u4e2d\u4e4b\u4e00\u5c31\u662f <strong>NSTimer<\/strong>\u3002\u9019\u500b\u5de5\u5177\u53ef\u4ee5\u8b93\u4f60\u5728\u6307\u5b9a\u7684\u6642\u9593\u9593\u9694\u5167\u57f7\u884c\u67d0\u500b\u52d5\u4f5c\uff0c\u7121\u8ad6\u662f\u6bcf\u9694\u4e00\u6bb5\u6642\u9593\u57f7\u884c\u4e00\u6b21\u67d0\u500b\u51fd\u5f0f\uff0c\u9084\u662f\u8a2d\u5b9a\u4e00\u500b\u5ef6\u9072\u57f7\u884c\u7684\u4efb\u52d9\u3002\u4e86\u89e3\u5982\u4f55\u6b63\u78ba\u4f7f\u7528 NSTimer \u5c0d\u65bc iOS \u958b\u767c\u8005\u4f86\u8aaa\u81f3\u95dc\u91cd\u8981\u3002<\/p>\n<h2>2025 \u6700\u65b0\u7684 NSTimer \u4f7f\u7528\u65b9\u5f0f<\/h2>\n<p>\u5728 Swift \u4e2d\uff0cNSTimer \u7684\u4f7f\u7528\u8a9e\u6cd5\u5df2\u7d93\u6709\u6240\u66f4\u65b0\uff0c\u4ee5\u4e0b\u662f\u5efa\u7acb\u4e00\u500b\u5b9a\u6642\u5668\u7684\u6700\u65b0\u7a0b\u5f0f\u78bc\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let timer = Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { timer in\n    self.update()\n}<\/code><\/pre>\n<p>\u4e0a\u9762\u7684\u7a0b\u5f0f\u78bc\u6703\u5efa\u7acb\u4e00\u500b NSTimer\uff0c\u6bcf\u9694 1 \u79d2\u57f7\u884c\u4e00\u6b21 <code>update<\/code> \u51fd\u5f0f\u3002\u9019\u662f\u4e00\u7a2e\u4f7f\u7528\u9589\u5305 (closure) \u7684\u65b9\u5f0f\u4f86\u8655\u7406\u5b9a\u6642\u5668\uff0c\u8b93\u7a0b\u5f0f\u78bc\u66f4\u52a0\u7c21\u6f54\u548c\u73fe\u4ee3\u5316\u3002<\/p>\n<h3>\u5b9a\u7fa9 Update \u51fd\u5f0f<\/h3>\n<pre><code class=\"language-swift line-numbers\">func update() {\n    print(\"Timer fired!\")\n}<\/code><\/pre>\n<h2>\u5982\u4f55\u505c\u6b62 NSTimer<\/h2>\n<p>\u7576\u4f60\u4e0d\u518d\u9700\u8981\u5b9a\u6642\u5668\u6642\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>invalidate<\/code> \u65b9\u6cd5\u4f86\u505c\u6b62\u57f7\u884c\u3002\u8acb\u6ce8\u610f\uff0c\u4e00\u65e6\u5b9a\u6642\u5668\u88ab\u7121\u6548\u5316\uff0c\u5c31\u4e0d\u80fd\u518d\u6b21\u4f7f\u7528\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">timer.invalidate()<\/code><\/pre>\n<h2>\u932f\u8aa4\u6392\u9664<\/h2>\n<p>\u5982\u679c\u4f60\u7684\u5b9a\u6642\u5668\u672a\u6309\u9810\u671f\u904b\u4f5c\uff0c\u8acb\u6aa2\u67e5\u4ee5\u4e0b\u5e7e\u9ede\uff1a<\/p>\n<ul>\n<li>\u78ba\u4fdd <code>invalidate<\/code> \u65b9\u6cd5\u672a\u88ab\u610f\u5916\u8abf\u7528\u3002<\/li>\n<li>\u6aa2\u67e5\u5b9a\u6642\u5668\u7684\u6642\u9593\u9593\u9694\u662f\u5426\u8a2d\u5b9a\u6b63\u78ba\u3002<\/li>\n<li>\u78ba\u8a8d <code>update<\/code> \u51fd\u5f0f\u662f\u5426\u6b63\u78ba\u88ab\u5b9a\u7fa9\u4e26\u80fd\u6b63\u5e38\u57f7\u884c\u3002<\/li>\n<\/ul>\n<h2>\u5ef6\u4f38\u61c9\u7528\u6848\u4f8b<\/h2>\n<p>NSTimer \u53ef\u4ee5\u88ab\u61c9\u7528\u65bc\u591a\u7a2e\u60c5\u5883\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li>\u904a\u6232\u4e2d\u7684\u8a08\u6642\u5668\uff0c\u7528\u65bc\u5012\u6578\u8a08\u6642\u6216\u66f4\u65b0\u904a\u6232\u72c0\u614b\u3002<\/li>\n<li>\u5b9a\u671f\u66f4\u65b0 UI \u5143\u7d20\uff0c\u5982\u986f\u793a\u7576\u524d\u6642\u9593\u6216\u8a08\u7b97\u9032\u5ea6\u3002<\/li>\n<li>\u5be6\u73fe\u81ea\u52d5\u6efe\u52d5\u7684\u65b0\u805e\u5217\u8868\u6216\u5716\u7247\u756b\u5eca\u3002<\/li>\n<\/ul>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u5b9a\u6642\u5668\uff1aNSTimer-\ud83d\udd70\ufe0f_1675396723.9817128.webp\" alt=\"Swift \u5b9a\u6642\u5668\uff1aNSTimer \ud83d\udd70\ufe0f\" title=\"Swift \u5b9a\u6642\u5668\u3001NSTimer\u3001\u5b9a\u6642\u5668\u529f\u80fd\u3001Swift \u958b\u767c\u3001\u61c9\u7528\u7a0b\u5f0f\u958b\u767c\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>Q1: NSTimer \u548c Timer \u6709\u4ec0\u9ebc\u4e0d\u540c\uff1f<\/h3>\n<p>A1: NSTimer \u662f\u820a\u7248\u7684 API\uff0c\u800c Timer \u662f Swift 4 \u4e4b\u5f8c\u7684\u66f4\u65b0\u7248\u672c\uff0c\u5efa\u8b70\u4f7f\u7528 Timer \u4f86\u5be6\u73fe\u76f8\u540c\u7684\u529f\u80fd\u3002<\/p>\n<h3>Q2: \u5982\u4f55\u5728\u4e3b\u57f7\u884c\u7dd2\u5916\u4f7f\u7528 NSTimer\uff1f<\/h3>\n<p>A2: \u4f7f\u7528 <code>DispatchQueue<\/code> \u6216\u8005\u8a2d\u5b9a\u5b9a\u6642\u5668\u7684 target \u70ba\u5176\u4ed6\u57f7\u884c\u7dd2\uff0c\u4f46\u8981\u78ba\u4fdd UI \u66f4\u65b0\u4ecd\u5728\u4e3b\u57f7\u884c\u7dd2\u9032\u884c\u3002<\/p>\n<h3>Q3: NSTimer \u7684\u9810\u8a2d\u884c\u70ba\u662f\u4ec0\u9ebc\uff1f<\/h3>\n<p>A3: NSTimer \u9ed8\u8a8d\u662f\u5728\u6dfb\u52a0\u5230\u904b\u884c\u5faa\u74b0\u5f8c\u958b\u59cb\u8a08\u6642\uff0c\u4e26\u4e14\u9700\u8981\u624b\u52d5\u7121\u6548\u5316\u4f86\u505c\u6b62\u5b83\u7684\u57f7\u884c\u3002<\/p>\n<p>\u900f\u904e\u9019\u7bc7\u6587\u7ae0\uff0c\u4f60\u61c9\u8a72\u5c0d Swift \u4e2d\u7684 NSTimer \u6709\u4e86\u66f4\u6df1\u5165\u7684\u4e86\u89e3\uff0c\u4e26\u80fd\u5920\u5728\u81ea\u5df1\u7684\u5c08\u6848\u4e2d\u9748\u6d3b\u904b\u7528\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u4ecb\u7d39Swift\u4e2dNSTimer\u7684\u5b9a\u6642\u5668\u529f\u80fd\uff0c\u8b93\u958b\u767c\u8005\u53ef\u4ee5\u66f4\u8f15\u9b06\u5730\u638c\u63e1\u5b9a\u6642\u5668\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u4e26\u66f4\u6709\u6548\u5730\u5229\u7528\u5b83\u4f86\u5b8c\u6210\u61c9\u7528\u7a0b\u5f0f\u7684\u958b\u767c\u3002<\/p>\n","protected":false},"author":1,"featured_media":6837,"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-6838","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-\u5b9a\u6642\u5668\uff1aNSTimer-\ud83d\udd70\ufe0f_1675396723.9817128.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1Mi","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6838","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=6838"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6838\/revisions"}],"predecessor-version":[{"id":13567,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6838\/revisions\/13567"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/6837"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=6838"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=6838"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=6838"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}