※ 引述《issuemylove (NotLove)》之铭言:
: 各位好
: 我利用AFNetworking写了个重复地询问server状态的连线(程式码在下行网址)
: http://pastebin.com/AYUDgK2j
: 想问说,一般网络上看到的情况是
: viewController A retain 一个 block B
: B 中用到 A 时要使用__weak才不会造成memory leak
: 程式码是我写的,可是我搞不清楚这样写对不对
: 感觉是 AFNetworking A, retain Block B, B 再 retain 自己写的Api C
: 然后 C 又retain B,...其实我搞糊涂了真正的retain方向没个概念
: 另外我这样写是正确的吗? 谢谢各位出手相救!
我举个例子:有 FirstViewController 与 SecondViewController,接口如下面网址:
https://gist.github.com/Darktt/b42ddc12d9ae049f0431f93775a756de
FirstViewController 会在其中一次的按钮触发下建立 SecondViewController,
并将 FirstViewController 给了 SecondViewController,
之后将它显示出来。
这时候 FirstViewController 其实还在内存中,不会被释放,
所以不用再使用 retain (strong) 将 FirstViewController 增加一次的 retain count,
这时候在 SecondViewController dealloc 的时候也不能释放 FirstViewController,
因为这会造成提早释放的问题。
其实 assign(weak) 与 retain(strong) 的差异是这个物件有没有被其他的物件长期持有
有的话就是 assign(weak),没有就是 retain(strong)。