美文网首页
ARC下block,weak,unsafe_unretained

ARC下block,weak,unsafe_unretained

作者: seventhboy | 来源:发表于2017-02-06 12:01 被阅读19次

ARC 下 block 依然可用 但是实现发现 block 在AF 执行异步多线程下 会调用返回后 才会调用dealloc将当前VC 销毁, 如果改为__weak 后 当前的VC 会马上调用dealloc销毁. 而网络请求会以错误error.code -999 异步取消返回.

__weak UIViewController *vc = self; // OK for iOS 5 only

__unsafe_unretained UIViewController *vc = self; // OK for iOS 4.x and up

__block UIViewController *vc = self; // OK if you aren't using ARC

以上是三个比较接近容易搞混的关键字.

__weak 在释放指针后能够同时将指针置为nil

__unsafe_unretained 只留下一个无效的也指针

__block 打破循环 copy副本 内部修改

相关文章

网友评论

      本文标题:ARC下block,weak,unsafe_unretained

      本文链接:https://www.haomeiwen.com/subject/rawyittx.html