Date
Dec. 22nd, 2024
 
2024年 11月 22日

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.
反曲点科技创始人和首席执行官。
开发、设计与写作皆为所长。
热爱苹果、钟情色彩。
随时恭候 垂询