{"id":3893,"date":"2025-06-04T10:49:37","date_gmt":"2025-06-05T03:44:37","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3893"},"modified":"2025-06-05T10:49:37","modified_gmt":"2025-06-05T03:44:37","slug":"%e5%ad%b8%e7%bf%92swift%e4%b8%ad%e7%9a%84%e5%86%85%e5%ad%98%e7%ae%a1%e7%90%86%ef%bc%9aarc%e3%80%81%e5%bc%b7%e5%bc%95%e7%94%a8%e7%92%b0%e5%92%8c%e5%bc%b1%e5%bc%95%e7%94%a8","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/swift\/%e5%ad%b8%e7%bf%92swift%e4%b8%ad%e7%9a%84%e5%86%85%e5%ad%98%e7%ae%a1%e7%90%86%ef%bc%9aarc%e3%80%81%e5%bc%b7%e5%bc%95%e7%94%a8%e7%92%b0%e5%92%8c%e5%bc%b1%e5%bc%95%e7%94%a8\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3Swift\u4e2d\u7684\u5167\u5b58\u7ba1\u7406\uff1aARC\u3001\u5f37\u5f15\u7528\u74b0\u8207\u5f31\u5f15\u7528\u7684\u6700\u4f73\u5be6\u8e10\uff082025\u6700\u65b0\uff09"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Swift, \u5167\u5b58\u7ba1\u7406, ARC, \u5f37\u5f15\u7528\u74b0, \u5f31\u5f15\u7528, Swift\u6700\u4f73\u5be6\u8e10\"><\/p>\n<h1>\u6df1\u5165\u4e86\u89e3Swift\u4e2d\u7684\u5167\u5b58\u7ba1\u7406\uff1aARC\u3001\u5f37\u5f15\u7528\u74b0\u8207\u5f31\u5f15\u7528\u7684\u6700\u4f73\u5be6\u8e10\uff082025\u6700\u65b0\uff09<\/h1>\n<p>\u57282025\u5e74\uff0cSwift\u5df2\u6210\u70ba\u958b\u767c\u8005\u4f7f\u7528\u6700\u5ee3\u6cdb\u7684\u7a0b\u5f0f\u8a9e\u8a00\u4e4b\u4e00\uff0c\u800c\u5167\u5b58\u7ba1\u7406\u5247\u662f\u6bcf\u4f4dSwift\u958b\u767c\u8005\u5fc5\u9808\u638c\u63e1\u7684\u91cd\u8981\u6982\u5ff5\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0eSwift\u4e2d\u7684\u81ea\u52d5\u5f15\u7528\u8a08\u6578\uff08ARC\uff09\u3001\u5f37\u5f15\u7528\u74b0\u548c\u5f31\u5f15\u7528\uff0c\u4e26\u63d0\u4f9b2025\u5e74\u7684\u6700\u65b0\u8a9e\u6cd5\u8207\u6700\u4f73\u5be6\u8e10\uff0c\u5e6b\u52a9\u60a8\u66f4\u6709\u6548\u5730\u7ba1\u7406\u5167\u5b58\u3002<\/p>\n<p><!--more--><\/p>\n<h2>\u81ea\u52d5\u5f15\u7528\u8a08\u6578\uff08ARC\uff09<\/h2>\n<p>\u81ea\u52d5\u5f15\u7528\u8a08\u6578\uff08ARC\uff09\u662fSwift\u4e2d\u6700\u6838\u5fc3\u7684\u5167\u5b58\u7ba1\u7406\u65b9\u5f0f\u3002\u5b83\u901a\u904e\u81ea\u52d5\u8a08\u7b97\u5c0d\u8c61\u7684\u5f15\u7528\u6b21\u6578\uff0c\u4f86\u78ba\u4fdd\u5167\u5b58\u7684\u6709\u6548\u4f7f\u7528\u3002\u4ee5\u4e0b\u662fARC\u7684\u5de5\u4f5c\u539f\u7406\u8207\u5be6\u4f5c\u793a\u4f8b\uff1a<\/p>\n<p>1. **\u5f15\u7528\u8a08\u6578\u7ba1\u7406**\uff1a\u6bcf\u7576\u5c0d\u8c61\u88ab\u5275\u5efa\u6642\uff0c\u5b83\u7684\u5f15\u7528\u8a08\u6578\u8a2d\u70ba1\u3002\u7576\u5176\u5b83\u5c0d\u8c61\u5f15\u7528\u5b83\u6642\uff0c\u8a08\u6578\u589e\u52a0\uff1b\u7576\u5f15\u7528\u4e0d\u518d\u5b58\u5728\u6642\uff0c\u8a08\u6578\u6e1b\u5c11\u3002<\/p>\n<p>2. **\u81ea\u52d5\u91cb\u653e**\uff1a\u7576\u8a08\u6578\u964d\u81f30\u6642\uff0c\u8a72\u5c0d\u8c61\u7684\u5167\u5b58\u5c07\u81ea\u52d5\u91cb\u653e\u3002<\/p>\n<p>&#8220;`swift<br \/>\nclass Person {<br \/>\n    var name: String<br \/>\n    init(name: String) {<br \/>\n        self.name = name<br \/>\n    }<br \/>\n}<\/p>\n<p>var john: Person? = Person(name: &#8220;John&#8221;)<br \/>\nprint(&#8220;\u5f15\u7528\u8a08\u6578\uff1a\\(CFGetRetainCount(john!))&#8221;) \/\/ \u8f38\u51fa\u5f15\u7528\u8a08\u6578<br \/>\njohn = nil \/\/ \u91cb\u653e\u5167\u5b58<br \/>\n&#8220;`<\/p>\n<h2>\u5f37\u5f15\u7528\u74b0<\/h2>\n<p>\u5f37\u5f15\u7528\u74b0\u662f\u6307\u5169\u500b\u6216\u591a\u500b\u5c0d\u8c61\u4e92\u76f8\u5f37\u5f15\u7528\uff0c\u5c0e\u81f4\u5b83\u5011\u6c38\u9060\u4e0d\u6703\u88ab\u91cb\u653e\u7684\u60c5\u6cc1\u3002\u9019\u6703\u5c0e\u81f4\u5167\u5b58\u6cc4\u6f0f\uff0c\u5c24\u5176\u662f\u5728\u4f7f\u7528\u9589\u5305\u6642\u3002\u4ee5\u4e0b\u662f\u89e3\u6c7a\u5f37\u5f15\u7528\u74b0\u7684\u4e00\u4e9b\u7b56\u7565\uff1a<\/p>\n<p>&#8211; **\u4f7f\u7528\u5f31\u5f15\u7528**\uff1a\u5728\u9589\u5305\u4e2d\u4f7f\u7528`[weak self]`\u4f86\u6253\u7834\u5f37\u5f15\u7528\u74b0\u3002<\/p>\n<p>&#8220;`swift<br \/>\nclass Teacher {<br \/>\n    var student: Student?<br \/>\n}<\/p>\n<p>class Student {<br \/>\n    var teacher: Teacher?<br \/>\n}<\/p>\n<p>var teacher: Teacher? = Teacher()<br \/>\nvar student: Student? = Student()<\/p>\n<p>teacher?.student = student<br \/>\nstudent?.teacher = teacher \/\/ \u9019\u88e1\u6703\u5f62\u6210\u5f37\u5f15\u7528\u74b0<\/p>\n<p>\/\/ \u4f7f\u7528\u5f31\u5f15\u7528<br \/>\nclass StudentWithWeakTeacher {<br \/>\n    weak var teacher: Teacher?<br \/>\n}<br \/>\n&#8220;`<\/p>\n<h2>\u5f31\u5f15\u7528<\/h2>\n<p>\u5f31\u5f15\u7528\u662f\u4e00\u7a2e\u4e0d\u589e\u52a0\u5c0d\u8c61\u5f15\u7528\u8a08\u6578\u7684\u5f15\u7528\u65b9\u5f0f\u3002\u9019\u4f7f\u5f97\u5b83\u5728\u9632\u6b62\u5167\u5b58\u6cc4\u6f0f\u65b9\u9762\u975e\u5e38\u6709\u7528\u3002\u7576\u4f7f\u7528\u5f31\u5f15\u7528\u6642\uff0c\u5982\u679c\u5c0d\u8c61\u88ab\u91cb\u653e\uff0c\u5f15\u7528\u5c07\u81ea\u52d5\u8a2d\u70banil\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528\u5f31\u5f15\u7528\u7684\u793a\u4f8b\uff1a<\/p>\n<p>&#8220;`swift<br \/>\nclass Company {<br \/>\n    var employee: Employee?<br \/>\n}<\/p>\n<p>class Employee {<br \/>\n    weak var company: Company? \/\/ \u4f7f\u7528\u5f31\u5f15\u7528\u9632\u6b62\u5f37\u5f15\u7528\u74b0<br \/>\n}<\/p>\n<p>var company: Company? = Company()<br \/>\nvar employee: Employee? = Employee()<\/p>\n<p>company?.employee = employee<br \/>\nemployee?.company = company \/\/ \u4e0d\u6703\u5f62\u6210\u5f37\u5f15\u7528\u74b0<br \/>\n&#8220;`<\/p>\n<h2>\u932f\u8aa4\u6392\u9664\u8207\u5ef6\u4f38\u61c9\u7528<\/h2>\n<p>\u5728\u958b\u767c\u904e\u7a0b\u4e2d\uff0c\u60a8\u53ef\u80fd\u6703\u9047\u5230\u4ee5\u4e0b\u932f\u8aa4\uff1a<\/p>\n<p>&#8211; **\u5167\u5b58\u6cc4\u6f0f**\uff1a\u78ba\u4fdd\u6aa2\u67e5\u5f37\u5f15\u7528\u74b0\uff0c\u4e26\u4f7f\u7528\u5f31\u5f15\u7528\u6253\u7834\u5faa\u74b0\u3002<br \/>\n&#8211; **\u610f\u5916\u91cb\u653e**\uff1a\u4f7f\u7528`unowned`\u4f86\u4fdd\u6301\u5c0d\u8c61\u7684\u5f15\u7528\u3002<\/p>\n<p>\u6b64\u5916\uff0c\u60a8\u53ef\u4ee5\u8003\u616e\u4f7f\u7528\u5de5\u5177\u5982Xcode\u7684\u5167\u5b58\u5716\uff08Memory Graph\uff09\u4f86\u6aa2\u67e5\u5167\u5b58\u4f7f\u7528\u60c5\u6cc1\uff0c\u6709\u6548\u627e\u51fa\u5167\u5b58\u6cc4\u6f0f\u3002<\/p>\n<h2>\u7e3d\u7d50<\/h2>\n<p>Swift\u4e2d\u7684\u5167\u5b58\u7ba1\u7406\u662f\u958b\u767c\u904e\u7a0b\u4e2d\u81f3\u95dc\u91cd\u8981\u7684\u4e00\u74b0\u3002\u901a\u904e\u81ea\u52d5\u5f15\u7528\u8a08\u6578\uff08ARC\uff09\u3001\u5f37\u5f15\u7528\u74b0\u548c\u5f31\u5f15\u7528\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u6709\u6548\u7ba1\u7406\u5167\u5b58\uff0c\u907f\u514d\u5167\u5b58\u6cc4\u6f0f\u3002\u9075\u5faa\u9019\u4e9b\u6700\u4f73\u5be6\u8e10\uff0c\u5c07\u6709\u52a9\u65bc\u63d0\u9ad8\u61c9\u7528\u7684\u6027\u80fd\u8207\u7a69\u5b9a\u6027\u3002<\/p>\n<h2>Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/h2>\n<p>**Q1: \u4ec0\u9ebc\u662f\u81ea\u52d5\u5f15\u7528\u8a08\u6578\uff08ARC\uff09\uff1f**<br \/>\nA1: \u81ea\u52d5\u5f15\u7528\u8a08\u6578\uff08ARC\uff09\u662fSwift\u4e2d\u7528\u4f86\u81ea\u52d5\u7ba1\u7406\u5167\u5b58\u7684\u6a5f\u5236\uff0c\u5b83\u901a\u904e\u8a08\u7b97\u5c0d\u8c61\u7684\u5f15\u7528\u6b21\u6578\u4f86\u78ba\u5b9a\u4f55\u6642\u91cb\u653e\u5167\u5b58\u3002<\/p>\n<p>**Q2: \u5982\u4f55\u907f\u514d\u5f37\u5f15\u7528\u74b0\uff1f**<br \/>\nA2: \u53ef\u4ee5\u901a\u904e\u4f7f\u7528\u5f31\u5f15\u7528\u6216\u4f7f\u7528`unowned`\u53c3\u8003\u4f86\u907f\u514d\u5f37\u5f15\u7528\u74b0\u3002<\/p>\n<p>**Q3: \u4ec0\u9ebc\u6642\u5019\u61c9\u8a72\u4f7f\u7528\u5f31\u5f15\u7528\u800c\u4e0d\u662f\u5f37\u5f15\u7528\uff1f**<br \/>\nA3: \u7576\u60a8\u9700\u8981\u9632\u6b62\u5faa\u74b0\u5f15\u7528\uff0c\u6216\u8005\u5e0c\u671b\u5728\u5c0d\u8c61\u91cb\u653e\u5f8c\u81ea\u52d5\u8a2d\u70banil\u6642\uff0c\u61c9\u4f7f\u7528\u5f31\u5f15\u7528\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6458\u8981\uff1a\u672c\u6587\u5c07\u4ecb\u7d39Swift\u4e2d\u7684\u5185\u5b58\u7ba1\u7406\uff0c\u5305\u62ec\u81ea\u52d5\u53c3\u8003\u8a08\u6578\uff08ARC\uff09\u3001\u5f37\u5f15\u7528\u74b0\u548c\u5f31\u5f15\u7528\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528\u5b83\u5011\u4f86\u7ba1\u7406\u5185\u5b58\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","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-3893","post","type-post","status-publish","format-standard","hentry","category-ios","category-swift","tag-ios","tag-swift"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-10N","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3893","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=3893"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3893\/revisions"}],"predecessor-version":[{"id":3923,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3893\/revisions\/3923"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=3893"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3893"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3893"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}