美文网首页iOS面试
终于理解weakSelf和strongSelf了

终于理解weakSelf和strongSelf了

作者: AlwaysBlue | 来源:发表于2020-05-11 15:23 被阅读0次

https://www.jianshu.com/p/79d31eb822db
https://www.jianshu.com/p/d68d5bb431c8
https://www.jianshu.com/p/701da54bd78c

// MRC

xcrun -sdk iphoneos clang -arch arm64 -rewrite-objc main.m

// ARC

xcrun -sdk iphoneos clang -arch arm64 -rewrite-objc -fobjc-arc -fobjc-runtime=ios-8.0.0 main.m

block内的__strong是会对对象retain +1的

这个问题的关键是理解:
1.__strong typeof(weakPerson) strongPerson = weakPerson;是可以对弱引用的对象retain+1的。
2.只有产生了循环引用才会内部不会释放     只要保证block内使用的strong对象引用计数不是0即可。

该例子并没有循环引用,但是在block内对self+1了

3.不用担心引用循环引用而产生内存泄漏,是因为strongPerson是个局部变量,block执行完毕就会被销毁,循环被破坏。

相关文章

  • 终于理解weakSelf和strongSelf了

    https://www.jianshu.com/p/79d31eb822dbhttps://www.jianshu...

  • RN 调 js 核心代码

    ^{RCTJSCExecutor *strongSelf = weakSelf;if (!strongSelf |...

  • 宏定义

    1.weakSelf和strongSelf

  • iOS宏定义

    1 weakself和strongself #ifndef weakify #if DEBUG #ifhas_fe...

  • weakSelf和strongSelf

    https://www.jianshu.com/p/51bb714051ea 我们发现这样确实解决了问题,但是可能...

  • weakSelf和strongSelf

    在block中常常会用到weakSelf和strong来处理block的产生循环引用的问题。 使用情况 直接在 b...

  • StrongSelf和WeakSelf

    我们在研发的过程中,为了避免循环引用常常会用weak若饮用来打破循环链 但是有时候,在异步多任务的时候,为了避免w...

  • 不一样的书写样式

    **********************1.block中weakSelf 与 strongSelf******...

  • StrongSelf

    weakSelf : 防止循环引用 strongSelf: 防止释放 需要 强引用weakSelf,主要是处理一...

  • Block使用规则

    ARC下,关于block里面使用self、weakSelf、strongSelf,理解一直没有特别的透彻,今天花时...

网友评论

    本文标题:终于理解weakSelf和strongSelf了

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