Date
Sep. 8th, 2024
 
2024年 8月 6日

Post: iOS 面试题: Retain Cycle

iOS 面试题: Retain Cycle

Published 12:10 Oct 14, 2015.

Created by @ezra. Categorized in #Programming, and tagged as #iOS.

Source format: Markdown

Table of Content

最近遇到和看到的一些面试题。

描述一个你遇到过的 Retain Cycle 例子。

block中的循环引用: 一个viewController

@property (nonatomic,strong)HttpRequestHandler * handler;
@property (nonatomic,strong)NSData             *data;
_handler = [httpRequestHandler sharedManager];
[_handler downloadData:^(id responseData){
    _data = responseData;
}];

self 拥有_handler, _handler 拥有block, block拥有self(因为使用了self的_data属性,block会copy 一份self)

解决方法:

__weak typedof(self)weakSelf = self
[_handler downloadData:^(id responseData){
    weakSelf.data = responseData;
}];
Pinned Message
HOTODOGO
The Founder and CEO of Infeca Technology.
Developer, Designer, Blogger.
Big fan of Apple, Love of colour.
Feel free to contact me.
反曲点科技创始人和首席执行官。
程序猿、设计师、奇怪的博主。
苹果死忠、热爱色彩斑斓的世界。
如有意向请随时 与我联系