Block automatic retaining, does it affect even to ivars in self?

*爱你&永不变心* 提交于 2019-12-06 05:54:40

问题


If I have class:

@interface A : NSObject
{
    BOOL b;
    id   c;
}
@end

and reference b and c in a block, is the block retain self automatically? Or just b and c? About c, it may be retained itself, but how about b?


回答1:


Bill's answer isn't quite correct:

If you have an instance of A and create a block inside of that instance like so:

^{
  b = YES;
}

Then self is retained (when the block is copied). b is not const-copied, because b is strongly referenced by self, and only self is const within the scope of the block.

On the other hand, if you do:

BOOL aBool = YES;
^{
  aBool = NO;
  c = [[NSObject alloc] init];
}

Then again, self is const-copied (and retained when the block itself is copied) and the assignment to c is allowed. However, the assignment to aBOOL is not allowed, because the value of aBool is const-copied.

In other words, the compiler recognizes the b and c are ivars, and will retain self instead of the ivars directly.


One way to think about this that helps me understand what's going on is to remember that an object is really just a fancy struct, which means you can technically access ivars via the arrow operator: ->

So when you're accessing ivars:

b = YES;

is equivalent to:

self->b = YES;

In that light, it makes perfect sense why you have to retain self, but b is not const. It's because b is only a slim part of the "bigger picture", and in order to get b, you must necessarily include all of self as well (since copying part of a struct doesn't really make sense in this context).




回答2:


Code from the block would be helpful in answering but assuming you have something like this;

^(A *a) {
    [a doSomething];
}

a will be retained by the block and released when the block is released. Nothing outside the ordinary will happen with b and c.

If you have something like;

^(id c, BOOL b) {
    [c doSomethingElse];
}

Then c will be retained by the block and b will be captured by the block as a const value (i.e. you would get a compiler error for doing b = NO)

For more details see the docs;

http://developer.apple.com/library/ios/#documentation/cocoa/Conceptual/Blocks/Articles/bxVariables.html%23//apple_ref/doc/uid/TP40007502-CH6-SW1



来源:https://stackoverflow.com/questions/5807022/block-automatic-retaining-does-it-affect-even-to-ivars-in-self

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