{"id":6702,"date":"2025-05-30T12:52:28","date_gmt":"2025-06-05T04:57:27","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=6702"},"modified":"2025-06-05T12:52:28","modified_gmt":"2025-06-05T04:57:27","slug":"https-badgameshow-com-steven-29","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/https-badgameshow-com-steven-29\/","title":{"rendered":"Swift \u767c\u9001\u901a\u77e5\uff1a2025 \u6700\u65b0\u6559\u7a0b\u8207\u6700\u4f73\u5be6\u8e10 \ud83d\udd14"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"swift, \u901a\u77e5, \u767c\u9001, NotificationCenter, iOS, \u7a0b\u5f0f\u6559\u5b78\"><\/p>\n<h2>Swift \u767c\u9001\u901a\u77e5\uff1a2025 \u6700\u65b0\u6559\u7a0b\u8207\u6700\u4f73\u5be6\u8e10 \ud83d\udd14<\/h2>\n<p>\u5728 Swift \u4e2d\uff0c\u767c\u9001\u901a\u77e5\u662f\u4e00\u500b\u975e\u5e38\u7c21\u55ae\u4f46\u5f37\u5927\u7684\u4efb\u52d9\uff0c\u53ea\u9700\u8981\u5229\u7528 <code>NotificationCenter<\/code> \u985e\u5225\u5373\u53ef\u8f15\u9b06\u5b8c\u6210\u3002<code>NotificationCenter<\/code> \u662f\u4e00\u500b\u55ae\u4f8b\uff0c\u5141\u8a31\u4f60\u5728\u61c9\u7528\u7a0b\u5f0f\u7684\u4efb\u4f55\u5730\u65b9\u767c\u9001\u548c\u63a5\u6536\u901a\u77e5\uff0c\u9019\u5c0d\u65bc\u5be6\u73fe\u4e0d\u540c\u7d44\u4ef6\u4e4b\u9593\u7684\u901a\u4fe1\u975e\u5e38\u6709\u7528\u3002<\/p>\n<h3>\u5b9a\u7fa9\u901a\u77e5\u540d\u7a31<\/h3>\n<p>\u5728\u958b\u59cb\u767c\u9001\u901a\u77e5\u4e4b\u524d\uff0c\u9996\u5148\u9700\u8981\u5b9a\u7fa9\u4e00\u500b <code>Notification.Name<\/code>\uff0c\u9019\u662f\u4e00\u500b <code>String<\/code> \u7684\u503c\uff0c\u7528\u65bc\u6a19\u8b58\u4f60\u7684\u901a\u77e5\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">extension Notification.Name {\n    static let myNotification = Notification.Name(\"myNotification\")\n}\n<\/code><\/pre>\n<h3>\u767c\u9001\u901a\u77e5<\/h3>\n<p>\u63a5\u4e0b\u4f86\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>NotificationCenter<\/code> \u7684 <code>post<\/code> \u65b9\u6cd5\u767c\u9001\u901a\u77e5\u3002\u9019\u662f\u4e00\u500b\u57fa\u672c\u7684\u4f8b\u5b50\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">NotificationCenter.default.post(name: .myNotification, object: nil)\n<\/code><\/pre>\n<p>\u4f60\u4e5f\u53ef\u4ee5\u6dfb\u52a0\u4e00\u4e9b\u9644\u52a0\u4fe1\u606f\uff0c\u4f7f\u63a5\u6536\u8005\u80fd\u7372\u53d6\u66f4\u591a\u7684\u8cc7\u8a0a\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">let userInfo = [\"name\": \"John Doe\"]\nNotificationCenter.default.post(name: .myNotification, object: nil, userInfo: userInfo)\n<\/code><\/pre>\n<h3>\u63a5\u6536\u901a\u77e5<\/h3>\n<p>\u8981\u63a5\u6536\u901a\u77e5\uff0c\u9700\u8981\u4f7f\u7528 <code>NotificationCenter<\/code> \u7684 <code>addObserver<\/code> \u65b9\u6cd5\uff0c\u4e26\u6307\u5b9a\u8981\u57f7\u884c\u7684\u9078\u64c7\u5668\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">NotificationCenter.default.addObserver(self, selector: #selector(handleNotification(_:)), name: .myNotification, object: nil)\n\n@objc func handleNotification(_ notification: Notification) {\n    \/\/ \u8655\u7406\u901a\u77e5\n}\n<\/code><\/pre>\n<p>\u5728\u63a5\u6536\u5230\u901a\u77e5\u5f8c\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>notification.userInfo<\/code> \u5c6c\u6027\u4f86\u7372\u53d6\u9644\u52a0\u4fe1\u606f\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">if let userInfo = notification.userInfo, let name = userInfo[\"name\"] as? String {\n    print(\"Received notification from \\(name)\")\n}\n<\/code><\/pre>\n<h3>\u79fb\u9664\u89c0\u5bdf\u8005<\/h3>\n<p>\u6700\u5f8c\uff0c\u7576\u4e0d\u518d\u9700\u8981\u63a5\u6536\u901a\u77e5\u6642\uff0c\u8a18\u5f97\u8981\u79fb\u9664\u89c0\u5bdf\u8005\uff0c\u4ee5\u907f\u514d\u6f5b\u5728\u7684\u5167\u5b58\u6cc4\u6f0f\uff1a<\/p>\n<pre><code class=\"language-swift line-numbers\">NotificationCenter.default.removeObserver(self)\n<\/code><\/pre>\n<h3>\u932f\u8aa4\u6392\u9664<\/h3>\n<p>\u5982\u679c\u767c\u73fe\u901a\u77e5\u6c92\u6709\u88ab\u63a5\u6536\uff0c\u8acb\u6aa2\u67e5\u4ee5\u4e0b\u5e7e\u9ede\uff1a<br \/>\n&#8211; \u78ba\u4fdd <code>addObserver<\/code> \u65b9\u6cd5\u5728\u767c\u9001\u901a\u77e5\u4e4b\u524d\u88ab\u8abf\u7528\u3002<br \/>\n&#8211; \u6aa2\u67e5\u6240\u4f7f\u7528\u7684\u901a\u77e5\u540d\u7a31\u662f\u5426\u6b63\u78ba\u3002<br \/>\n&#8211; \u78ba\u4fdd\u89c0\u5bdf\u8005\u5728\u63a5\u6536\u901a\u77e5\u6642\u4ecd\u7136\u5b58\u5728\u3002<\/p>\n<h3>\u5ef6\u4f38\u61c9\u7528<\/h3>\n<p>\u4f7f\u7528 <code>NotificationCenter<\/code> \u53ef\u4ee5\u5be6\u73fe\u66f4\u8907\u96dc\u7684\u529f\u80fd\uff0c\u6bd4\u5982\u81ea\u5b9a\u7fa9\u7684 UI \u66f4\u65b0\u3001\u5be6\u73fe\u4e0d\u540c\u6a21\u584a\u9593\u7684\u901a\u4fe1\u7b49\u3002\u4f60\u53ef\u4ee5\u5728\u61c9\u7528\u4e2d\u9748\u6d3b\u904b\u7528\u9019\u500b\u529f\u80fd\u4f86\u63d0\u5347\u7528\u6236\u9ad4\u9a57\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2023\/02\/Swift-\u767c\u9001\u901a\u77e5-\ud83d\udd14_1675394136.9666061.webp\" alt=\"Swift \u767c\u9001\u901a\u77e5 \ud83d\udd14\" title=\"Swift \u901a\u77e5, Swift NotificationCenter, \u767c\u9001\u901a\u77e5, \u63a5\u6536\u901a\u77e5, Swift \u7a0b\u5f0f\u8a9e\u6cd5\" width=\"1200\" height=\"628\"\/><\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<h3>1. \u70ba\u4ec0\u9ebc\u6211\u5728\u4f7f\u7528 NotificationCenter \u6642\u6536\u4e0d\u5230\u901a\u77e5\uff1f<\/h3>\n<p>\u8acb\u6aa2\u67e5\u89c0\u5bdf\u8005\u662f\u5426\u5df2\u6dfb\u52a0\uff0c\u4e26\u78ba\u8a8d\u6240\u4f7f\u7528\u7684\u901a\u77e5\u540d\u7a31\u662f\u5426\u6b63\u78ba\u3002<\/p>\n<h3>2. \u5982\u4f55\u5728 Swift \u4e2d\u4f7f\u7528 NotificationCenter \u50b3\u905e\u8cc7\u6599\uff1f<\/h3>\n<p>\u53ef\u4ee5\u901a\u904e <code>userInfo<\/code> \u5b57\u5178\u5728\u767c\u9001\u901a\u77e5\u6642\u50b3\u905e\u8cc7\u6599\uff0c\u63a5\u6536\u6642\u518d\u5f9e <code>notification.userInfo<\/code> \u4e2d\u7372\u53d6\u3002<\/p>\n<h3>3. \u79fb\u9664\u89c0\u5bdf\u8005\u5728\u54ea\u500b\u6642\u5019\u6700\u5408\u9069\uff1f<\/h3>\n<p>\u7576\u4e0d\u518d\u9700\u8981\u63a5\u6536\u901a\u77e5\u6642\uff0c\u6216\u8005\u5728 <code>deinit<\/code> \u65b9\u6cd5\u4e2d\u79fb\u9664\u89c0\u5bdf\u8005\uff0c\u4ee5\u907f\u514d\u5167\u5b58\u6cc4\u6f0f\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3\u5982\u4f55\u4f7f\u7528Swift\u767c\u9001\u901a\u77e5\ud83d\udd14\uff0c\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528Swift\u7684NotificationCenter\u4f86\u767c\u9001\u901a\u77e5\uff0c\u4ee5\u53ca\u5982\u4f55\u8a3b\u518a\u548c\u63a5\u6536\u901a\u77e5\u3002<\/p>\n","protected":false},"author":1,"featured_media":6701,"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-6702","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-\u767c\u9001\u901a\u77e5-\ud83d\udd14_1675394136.9666061.webp","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1K6","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6702","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=6702"}],"version-history":[{"count":0,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/6702\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/6701"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=6702"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=6702"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=6702"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}