{"id":7307,"date":"2022-09-08T11:40:58","date_gmt":"2022-09-08T03:40:58","guid":{"rendered":"http:\/\/123.57.164.21\/?p=7307"},"modified":"2022-09-08T11:40:58","modified_gmt":"2022-09-08T03:40:58","slug":"ios-%e9%97%ad%e5%8c%85%e9%9c%80%e8%a6%81%e4%bd%bf%e7%94%a8-weak-self-%e7%9a%84%e6%83%85%e5%86%b5","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=7307","title":{"rendered":"iOS \u95ed\u5305\u9700\u8981\u4f7f\u7528 weak self \u7684\u60c5\u51b5"},"content":{"rendered":"\n<p><strong>\u4ec0\u4e48\u662f\u5faa\u73af\u5f15\u7528\uff1f<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/09\/\u56fe\u7247-28.png\" alt=\"\" class=\"wp-image-7308\" width=\"386\" height=\"262\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/09\/\u56fe\u7247-28.png 654w, https:\/\/92it.top\/wp-content\/uploads\/2022\/09\/\u56fe\u7247-28-300x204.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/09\/\u56fe\u7247-28-230x156.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/09\/\u56fe\u7247-28-350x238.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2022\/09\/\u56fe\u7247-28-480x326.png 480w\" sizes=\"(max-width: 386px) 100vw, 386px\" \/><figcaption>\u5982\u56fe\uff0cA\u4e2d\u6709B\uff0cB\u4e2d\u6709A\uff1b\u5219\u5f53\u6211\u4eec\u521d\u59cb\u5316A\u4e4b\u540e\uff0ca.b\u548cb.a\u5f62\u6210\u4e86\u3010<strong>\u5f15\u7528\u5faa\u73af\u3011<\/strong><\/figcaption><\/figure><\/div>\n\n\n\n<p>\u8fd9\u662f\u6211\u4eec\u4e0d\u5e0c\u671b\u770b\u5230\u7684\uff0c\u56e0\u4e3a\u6b64\u65f6 A \u548c B \u4e24\u4e2a\u7c7b\u7684\u5b9e\u4f8b\u90fd\u4e0d\u80fd\u88ab deinit<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"456\" height=\"148\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2022\/09\/\u56fe\u7247-29.png\" alt=\"\" class=\"wp-image-7309\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2022\/09\/\u56fe\u7247-29.png 456w, https:\/\/92it.top\/wp-content\/uploads\/2022\/09\/\u56fe\u7247-29-300x97.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2022\/09\/\u56fe\u7247-29-230x75.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2022\/09\/\u56fe\u7247-29-350x114.png 350w\" sizes=\"(max-width: 456px) 100vw, 456px\" \/><figcaption>\u8fd9\u6837\u6539\u5199\u4e4b\u540e\uff0c A \u548c B \u4e24\u4e2a\u7c7b\u7684\u5b9e\u4f8b\u5c31\u90fd\u53ef\u4ee5\u88ab deinit \u4e86<\/figcaption><\/figure><\/div>\n\n\n\n<p>\u56e0\u4e3a\u6211\u4eec\u5728 var a\u524d\u9762\u52a0\u4e0a\u4e86 weak\uff0c\u5c31\u662f\u5411\u7f16\u8bd1\u5668\u8868\u660e\u6211\u4eec\u4e0d\u5e0c\u671b\u6301\u6709 a<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>\u95ed\u5305\u9700\u8981\u4f7f\u7528 weak self \u7684\u60c5\u51b5<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u53d1\u751f\u8fd9\u79cd\u60c5\u51b5\u7684\u4e3b\u8981\u539f\u56e0\u662fself\u6301\u6709\u4e86closure,\u800cclosure\u6709\u6301\u6709\u4e86self,\u6240\u4ee5\u5c31\u9020\u6210\u4e86\u5faa\u73af\u5f15\u7528,\u4ece\u800c\u5c0f\u660e\u5bf9\u8c61\u6ca1\u6709\u88ab\u91ca\u653e\u3002<\/p>\n\n\n\n<p>\u4e00\u53e5\u8bdd\u89e3\u91ca\uff1a\u53ea\u6709\u5f53block\u76f4\u63a5\u6216\u95f4\u63a5\u7684\u88abself\u6301\u6709\u65f6\uff0c\u624d\u9700\u8981weak self\u3002<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ \u8fd9\u79cd\u60c5\u51b5\u6ca1\u5fc5\u8981\n[self fetchDataWithSucess:^{\n     [self doSomething];\n}];\n\n\/\/\u8fd9\u79cd\u60c5\u51b5\u5c31\u6709\u5fc5\u8981\nself.onTapEvent = ^{\n    [self doSomething];\n};\n<\/pre>\n\n\n\n<p>block\u4e2d\u4e3a\u4ec0\u4e48\u4f1a\u7528\u5230weakself\u662f\u56e0\u4e3a\u8981\u907f\u514d\u5faa\u73af\u5f15\u7528\uff0c\u4e00\u65e6\u51fa\u73b0\u5faa\u73af\u5f15\u7528\u90a3\u4e48\u5bf9\u8c61\u5c31\u4f1a\u5e38\u9a7b\u5185\u5b58\u3002\u5982\u679c\u4e00\u4e2a\u5e94\u7528\u7a0b\u5e8f\u91cc\u9762\u4f60\u6709\u5f88\u591a\u5faa\u73af\u5f15\u7528\uff0c\u90a3\u4e48\u5185\u5b58\u5360\u7528\u5c31\u4f1a\u6bd4\u8f83\u5927\uff0c\u8fd9\u5f53\u7136\u662f\u8c01\u90fd\u4e0d\u60f3\u770b\u5230\u7684\u7ed3\u679c\u3002\u90a3\u4e48\u95ee\u9898\u7684\u91cd\u70b9\u5c31\u662f\uff1a\u4ec0\u4e48\u65f6\u5019\u4f1a\u51fa\u73b0\u5faa\u73af\u5f15\u7528\uff1f\u518d\u5148\u6765\u770b\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">NSArray *anArray = @[@\"1\", @\"2\", @\"3\"];\n[anArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {\n    [self doSomething:idx];\n}];\n<\/pre>\n\n\n\n<p>\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0cblock\u4e2dretain\u4e86self\uff0c\u5f53block\u4e2d\u7684\u4ee3\u7801\u88ab\u6267\u884c\u5b8c\u540e\uff0cself\u5c31\u4f1a\u88abARC\u91ca\u653e\u3002\u6240\u4ee5\u4e0d\u9700\u8981\u5904\u7406weakself\u7684\u60c5\u51b5\u3002<\/p>\n\n\n\n<p>\u518d\u6765\u770b\u4e00\u4e2a\u4f8b\u5b50<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">@interface aViewController ()\n@property (nonatomic, strong) void(^aBlock)(id obj, NSUInteger idx, BOOL *stop);\n@end\n\n__weak aViewController *weakSelf = self;\nself.aBlock = ^(id obj, NSUInteger idx, BOOL *stop) {\n    [weakSelf doSomething:idx];\n}\n<\/pre>\n\n\n\n<p>\u8fd9\u4e2a\u4f8b\u5b50\u7684\u533a\u522b\u5728\u4e8e\uff1a<strong>block\u88abself strong\u5f15\u7528\u3002<\/strong>\u6240\u4ee5\u7ed3\u679c\u5c31\u662fblock\u4e2d\u5f15\u7528\u4e86self\uff0cself\u5f15\u7528\u4e86block\u3002\u90a3\u4e48\u8fd9\u4e2a\u65f6\u5019\uff0c\u5982\u679c\u4f60\u4e0d\u4f7f\u7528weakself\uff0c\u5219self\u548cblock\u6c38\u8fdc\u90fd\u4e0d\u4f1a\u88ab\u91ca\u653e\u3002<\/p>\n\n\n\n<p>\u90a3\u4e48\u662f\u4e0d\u662f\u9047\u5230block\u90fd\u8981\u4f7f\u7528weakself\u5462\uff1f\u5f53\u7136\u4e0d\u662f\uff0c\u800c\u4e14\u5982\u679c\u5168\u90e8\u4f7f\u7528weakself\uff0c\u4f1a\u51fa\u73b0\u4f60\u60f3\u6267\u884cblock\u4e2d\u7684\u4ee3\u7801\u65f6\uff0cself\u5df2\u7ecf\u88ab\u91ca\u653e\u6389\u4e86\u7684\u60c5\u51b5\u3002<\/p>\n\n\n\n<p>\u53e6\u5916\uff0c\u5728\u5904\u7406weakself\u65f6\uff0c\u6709\u4e24\u79cd\u505a\u6cd5\uff1a__weak\u548c__unsafe_unretained\u3002\u4e24\u79cd\u505a\u6cd5\u5404\u6709\u63a8\u8350\uff0c\u6709\u7684\u4eba\u89c9\u5f97\u540e\u8005\u4ece\u5b57\u9762\u4e0a\u66f4\u597d\u7406\u89e3\uff0c\u800c\u6709\u7684\u4eba\u89c9\u5f97\u524d\u8005\u66f4\u52a0\u5b89\u5168\uff0c\u56e0\u4e3aself\u88ab\u91ca\u653e\u65f6\u4f1a\u81ea\u52a8\u6307\u5411nil\u3002\u6709\u7684\u4eba\u53c8\u8bf4\u4e86\uff0c\u5c31\u662f\u5e94\u8be5\u8ba9app\u5d29\u6e83\u624d\u80fd\u53d1\u73b0\u95ee\u9898\u6240\u5728\u3002\u8fd9\u4e2a\uff0c\u770b\u4e2a\u4eba\u5427\u3002<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ec0\u4e48\u662f\u5faa\u73af\u5f15\u7528\uff1f \u8fd9\u662f\u6211\u4eec\u4e0d\u5e0c\u671b\u770b\u5230\u7684\uff0c\u56e0\u4e3a\u6b64\u65f6 A \u548c B \u4e24\u4e2a\u7c7b\u7684\u5b9e\u4f8b\u90fd\u4e0d\u80fd\u88ab deinit \u56e0\u4e3a\u6211\u4eec\u5728  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6,8],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/7307"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7307"}],"version-history":[{"count":2,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/7307\/revisions"}],"predecessor-version":[{"id":7311,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/7307\/revisions\/7311"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7307"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7307"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7307"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}