{"id":7232,"date":"2025-06-01T12:18:11","date_gmt":"2025-06-05T04:39:10","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/uncategorized\/https-badgameshow-com-steven-289\/"},"modified":"2025-06-05T12:18:11","modified_gmt":"2025-06-05T04:39:10","slug":"https-badgameshow-com-steven-289","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-289\/","title":{"rendered":"Swift \u901a\u77e5\u529f\u80fd\u8a73\u89e3 &#8211; 2025 \u6700\u65b0\u63a5\u6536\u8207\u767c\u9001\u901a\u77e5\u7684\u6700\u4f73\u5be6\u8e10"},"content":{"rendered":"<p>&#8220;`html<br \/>\n<meta name=\"keywords\" content=\"swift, \u901a\u77e5\u529f\u80fd, NotificationCenter, Delegate, iOS\"><\/p>\n<h2>Swift \u901a\u77e5\u529f\u80fd\u6982\u8ff0<\/h2>\n<p>\u5728\u73fe\u4ee3\u61c9\u7528\u7a0b\u5f0f\u958b\u767c\u4e2d\uff0cSwift \u7684\u901a\u77e5\u529f\u80fd\u626e\u6f14\u8457\u95dc\u9375\u89d2\u8272\uff0c\u80fd\u5920\u8b93\u61c9\u7528\u7a0b\u5f0f\u6709\u6548\u5730\u63a5\u6536\u548c\u767c\u9001\u901a\u77e5\u3002\u9019\u88e1\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528 <strong>NotificationCenter<\/strong> \u548c <strong>Delegate<\/strong> \u5169\u7a2e\u65b9\u5f0f\u4f86\u5be6\u73fe\u9019\u4e00\u529f\u80fd\uff0c\u4e26\u63d0\u4f9b\u5b8c\u6574\u7684\u5be6\u4f5c\u7bc4\u4f8b\u548c\u6700\u4f73\u5be6\u8e10\uff0c\u4ee5\u5e6b\u52a9\u60a8\u57282025\u5e74\u53ca\u4ee5\u5f8c\u7684\u958b\u767c\u4e2d\u4fdd\u6301\u7af6\u722d\u529b\u3002<\/p>\n<h2>\u63a5\u6536\u901a\u77e5<\/h2>\n<p>\u63a5\u6536\u901a\u77e5\u7684\u65b9\u5f0f\u6709\u5169\u7a2e\uff1a\u4f7f\u7528 <strong>NotificationCenter<\/strong> \u6216\u4f7f\u7528 <strong>Delegate<\/strong>\u3002\u4ee5\u4e0b\u5c07\u8a73\u8ff0\u9019\u5169\u7a2e\u65b9\u6cd5\u3002<\/p>\n<h3>\u4f7f\u7528 NotificationCenter<\/h3>\n<p>\u4f7f\u7528 <strong>NotificationCenter<\/strong> \u63a5\u6536\u901a\u77e5\u7684\u6b65\u9a5f\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let notificationName = Notification.Name(\"MyNotification\")\n\nNotificationCenter.default.addObserver(self, selector: #selector(handleNotification), name: notificationName, object: nil)\n\n@objc func handleNotification(notification: Notification) {\n    print(\"Received notification: \\(notification.name)\")\n    \/\/ \u5728\u6b64\u8655\u57f7\u884c\u63a5\u6536\u5230\u901a\u77e5\u5f8c\u7684\u64cd\u4f5c\n}\n<\/code><\/pre>\n<p>\u5728\u4e0a\u9762\u7684\u7bc4\u4f8b\u4e2d\uff0c\u7576\u63a5\u6536\u5230\u540d\u70ba &#8220;MyNotification&#8221; \u7684\u901a\u77e5\u6642\uff0c\u7cfb\u7d71\u6703\u8abf\u7528 <code>handleNotification<\/code> \u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u5728\u9019\u88e1\u57f7\u884c\u76f8\u61c9\u7684\u696d\u52d9\u908f\u8f2f\u3002<\/p>\n<h3>\u4f7f\u7528 Delegate<\/h3>\n<p>\u4f7f\u7528 <strong>Delegate<\/strong> \u4f86\u63a5\u6536\u901a\u77e5\u7684\u6b65\u9a5f\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">protocol MyNotificationDelegate: AnyObject {\n    func handleNotification()\n}\n\nweak var delegate: MyNotificationDelegate?\n\n\/\/ \u7576\u9700\u8981\u901a\u77e5\u6642\ndelegate?.handleNotification()\n<\/code><\/pre>\n<p>\u5728\u9019\u7a2e\u65b9\u6cd5\u4e2d\uff0c\u60a8\u9700\u8981\u5b9a\u7fa9\u4e00\u500b\u5354\u5b9a <code>MyNotificationDelegate<\/code>\uff0c\u4e26\u5728\u9700\u8981\u901a\u77e5\u7684\u985e\u5225\u4e2d\u8abf\u7528 <code>delegate<\/code> \u5c6c\u6027\u4f86\u901a\u77e5\u76f8\u61c9\u7684\u7269\u4ef6\u3002<\/p>\n<h2>\u767c\u9001\u901a\u77e5<\/h2>\n<p>\u767c\u9001\u901a\u77e5\u7684\u65b9\u5f0f\u540c\u6a23\u6709\u5169\u7a2e\uff1a\u4f7f\u7528 <strong>NotificationCenter<\/strong> \u6216\u4f7f\u7528 <strong>Delegate<\/strong>\u3002<\/p>\n<h3>\u4f7f\u7528 NotificationCenter<\/h3>\n<p>\u767c\u9001\u901a\u77e5\u7684\u6b65\u9a5f\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let notificationName = Notification.Name(\"MyNotification\")\n\nNotificationCenter.default.post(name: notificationName, object: nil)\n<\/code><\/pre>\n<p>\u9019\u6bb5\u7a0b\u5f0f\u78bc\u6703\u5c07\u540d\u70ba &#8220;MyNotification&#8221; \u7684\u901a\u77e5\u767c\u9001\u5230\u6240\u6709\u8a3b\u518a\u4e86\u8a72\u901a\u77e5\u7684\u89c0\u5bdf\u8005\u3002<\/p>\n<h3>\u4f7f\u7528 Delegate<\/h3>\n<p>\u767c\u9001\u901a\u77e5\u7684\u6b65\u9a5f\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">protocol MyNotificationDelegate: AnyObject {\n    func handleNotification()\n}\n\nweak var delegate: MyNotificationDelegate?\n\n\/\/ \u7576\u9700\u8981\u767c\u9001\u901a\u77e5\u6642\ndelegate?.handleNotification()\n<\/code><\/pre>\n<p>\u9019\u6bb5\u7a0b\u5f0f\u78bc\u5c07\u8abf\u7528\u5be6\u73fe\u4e86 <code>MyNotificationDelegate<\/code> \u5354\u5b9a\u7684\u7269\u4ef6\u7684 <code>handleNotification<\/code> \u65b9\u6cd5\uff0c\u5be6\u73fe\u901a\u77e5\u7684\u767c\u9001\u3002<\/p>\n<h2>\u932f\u8aa4\u6392\u9664\u8207\u6700\u4f73\u5be6\u8e10<\/h2>\n<p>\u5728\u4f7f\u7528\u901a\u77e5\u529f\u80fd\u6642\u53ef\u80fd\u6703\u9047\u5230\u4e00\u4e9b\u5e38\u898b\u932f\u8aa4\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li>\u672a\u6b63\u78ba\u8a3b\u518a\u89c0\u5bdf\u8005\uff1a\u78ba\u4fdd\u5728\u9069\u7576\u7684\u751f\u547d\u9031\u671f\u65b9\u6cd5\u4e2d\u8a3b\u518a\u548c\u79fb\u9664\u89c0\u5bdf\u8005\u3002<\/li>\n<li>\u4f7f\u7528 <code>weak<\/code> \u53c3\u8003\u907f\u514d\u5faa\u74b0\u5f15\u7528\uff1a\u5728\u4f7f\u7528 Delegate \u6a21\u5f0f\u6642\uff0c\u7e3d\u662f\u4f7f\u7528 <code>weak<\/code> \u95dc\u9375\u5b57\u4f86\u9632\u6b62\u8a18\u61b6\u9ad4\u6cc4\u6f0f\u3002<\/li>\n<\/ul>\n<p>\u9019\u4e9b\u6700\u4f73\u5be6\u8e10\u5c07\u6709\u52a9\u65bc\u60a8\u63d0\u5347\u61c9\u7528\u7a0b\u5f0f\u7684\u6027\u80fd\u548c\u7a69\u5b9a\u6027\u3002<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>Swift \u7684\u901a\u77e5\u529f\u80fd\u63d0\u4f9b\u4e86\u9748\u6d3b\u7684\u65b9\u5f0f\u4f86\u63a5\u6536\u548c\u767c\u9001\u901a\u77e5\u3002\u7121\u8ad6\u662f\u4f7f\u7528 <strong>NotificationCenter<\/strong> \u9084\u662f <strong>Delegate<\/strong>\uff0c\u4e86\u89e3\u5176\u904b\u4f5c\u65b9\u5f0f\u548c\u6700\u4f73\u5be6\u8e10\u5c07\u6709\u52a9\u65bc\u63d0\u5347\u60a8\u7684\u958b\u767c\u6548\u7387\u548c\u61c9\u7528\u7a0b\u5f0f\u7684\u7528\u6236\u9ad4\u9a57\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u901a\u77e5\u529f\u80fd-\u63a5\u6536\u548c\u767c\u9001\u901a\u77e5\u7684\u5be6\u73fe\u65b9\u5f0f_1675409422.317139.webp\" alt=\"Swift \u901a\u77e5\u529f\u80fd - \u63a5\u6536\u548c\u767c\u9001\u901a\u77e5\u7684\u5be6\u73fe\u65b9\u5f0f\" title=\"Swift \u901a\u77e5\u529f\u80fd\u3001\u63a5\u6536\u901a\u77e5\u3001\u767c\u9001\u901a\u77e5\u3001\u7a0b\u5f0f\u78bc\u793a\u4f8b\u3001\u4f7f\u7528\u65b9\u5f0f\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>Q1: NotificationCenter \u548c Delegate \u6709\u4f55\u4e0d\u540c\uff1f<\/h3>\n<p>A1: NotificationCenter \u662f\u4e00\u7a2e\u5ee3\u64ad\u6a21\u578b\uff0c\u5141\u8a31\u591a\u500b\u89c0\u5bdf\u8005\u63a5\u6536\u540c\u4e00\u901a\u77e5\uff0c\u800c Delegate \u662f\u4e00\u5c0d\u4e00\u7684\u56de\u8abf\u6a5f\u5236\uff0c\u901a\u5e38\u7528\u65bc\u50b3\u905e\u7279\u5b9a\u4e8b\u4ef6\u7684\u8655\u7406\u3002<\/p>\n<h3>Q2: \u5982\u4f55\u907f\u514d\u5728\u4f7f\u7528 NotificationCenter \u6642\u51fa\u73fe\u8a18\u61b6\u9ad4\u6cc4\u6f0f\uff1f<\/h3>\n<p>A2: \u78ba\u4fdd\u5728\u9069\u7576\u7684\u6642\u5019\u4f7f\u7528 <code>removeObserver<\/code> \u65b9\u6cd5\u79fb\u9664\u89c0\u5bdf\u8005\uff0c\u4ee5\u9632\u6b62\u5728\u4e0d\u9700\u8981\u6642\u4ecd\u7136\u6301\u6709\u5c0d\u7269\u4ef6\u7684\u5f15\u7528\u3002<\/p>\n<p>&#8220;`<br \/>\n&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u4ecb\u7d39Swift\u4e2d\u7684\u901a\u77e5\u529f\u80fd\uff0c\u8a73\u7d30\u4ecb\u7d39\u5982\u4f55\u63a5\u6536\u548c\u767c\u9001\u901a\u77e5\uff0c\u4e26\u63d0\u4f9b\u7a0b\u5f0f\u78bc\u793a\u4f8b\uff0c\u8b93\u958b\u767c\u8005\u53ef\u4ee5\u66f4\u8f15\u9b06\u5730\u4f7f\u7528Swift\u901a\u77e5\u529f\u80fd\u3002<\/p>\n","protected":false},"author":1,"featured_media":7231,"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-7232","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-\u901a\u77e5\u529f\u80fd-\u63a5\u6536\u548c\u767c\u9001\u901a\u77e5\u7684\u5be6\u73fe\u65b9\u5f0f_1675409422.317139.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1SE","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7232","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=7232"}],"version-history":[{"count":0,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/7232\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/7231"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=7232"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=7232"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=7232"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}