Capturing a variable in a Block when the Block is in the initializer

非 Y 不嫁゛ 提交于 2019-11-30 03:59:50

问题


Consider this:

id observer = [[NSNotificationCenter defaultCenter] 
    addObserverForName:MyNotification 
                object:nil 
                 queue:nil 
            usingBlock:^(NSNotification *note) {
                [[NSNotificationCenter defaultCenter] 
                        removeObserver:observer 
                                  name:MyNotification 
                                object:nil
            ];
            // do other stuff here...
    }
];

I'm using this pattern to observe a notification once and then stop observing it. But LLVM tells me (under ARC) that Variable 'observer' is uninitialized when captured by block.

How can I fix this, since the block necessarily captures the variable before initialization, it being part of the initializer? Will using the __block qualifier on observer do the trick?


回答1:


As explained in the answers to

Why doesn't Remove Observer from NSNotificationCenter:addObserverForName:usingBlock get called,

you have to

  • add __block, so that the block will refer to the initialized variable, AND
  • add __weak, to avoid a retain cycle. (The latter applies only to ARC. Without ARC, the block does not create a strong reference to a __block variable.)

Therefore:

__block __weak id observer = [[NSNotificationCenter defaultCenter] ...



回答2:


yes, I think declaring observer beforehand as __block id observer; should work.




回答3:


Yes, using __block will solve the problem.

Without it, the Block gets a copy of the variable's value at the time the Block is created. (Which is "uninitialized" in this case.) With it, the Block (in essence) gets the variable itself, so that the value can be changed from within the Block. Thus it will also "track" changes made from outside.



来源:https://stackoverflow.com/questions/19527888/capturing-a-variable-in-a-block-when-the-block-is-in-the-initializer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!