简体   繁体   中英

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

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 ?

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).

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 (ie 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM