V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
SeanChense
V2EX  ›  iDev

block 中的循环引用问题

  •  
  •   SeanChense · 2015-08-06 20:32:02 +08:00 · 1797 次点击
    这是一个创建于 3200 天前的主题,其中的信息可能已经有所发展或是发生改变。

    https://gist.github.com/SeanChense/b4dfb7db4e983e88e963

    这样调用的话会引起 self 的循环引用吗?

    for app users

    6 条回复    2015-08-07 14:04:46 +08:00
    tane05
        1
    tane05  
       2015-08-06 20:38:37 +08:00 via iPhone   ❤️ 1
    不会,block捕获的是它作用域的变量.
    wujichao
        2
    wujichao  
       2015-08-06 20:58:37 +08:00
    加了weakself肯定不会
    但是 用self 也可能不会引起循环引用, 和 `foo handle:^{}` 的实现有关
    pengjay
        3
    pengjay  
       2015-08-06 21:34:22 +08:00
    不会
    SeanChense
        4
    SeanChense  
    OP
       2015-08-06 23:02:14 +08:00 via iPhone
    @wujichao 怎么讲?
    SeanChense
        5
    SeanChense  
    OP
       2015-08-06 23:02:46 +08:00 via iPhone
    @tane05
    @pengjay

    谢谢
    banxi1988
        6
    banxi1988  
       2015-08-07 14:04:46 +08:00   ❤️ 1
    一般比较更好一写法是:

    ```objc
    [foo handler: ^{
    id strongself = weakself;
    if(strongself){
    [strongself doSomething]; // 直接用 weakself 有可能 crash,因为其可能变成了野指针
    }
    }];
    ```
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2936 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 39ms · UTC 11:29 · PVG 19:29 · LAX 04:29 · JFK 07:29
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.