iOS: Assignment to iVar in Block (ARC)
Posted
by
manmal
on Stack Overflow
See other posts from Stack Overflow
or by manmal
Published on 2012-07-04T10:23:04Z
Indexed on
2012/07/04
21:16 UTC
Read the original article
Hit count: 273
I have a readonly property isFinished
in my interface file:
typedef void (^MyFinishedBlock)(BOOL success, NSError *e);
@interface TMSyncBase : NSObject {
BOOL isFinished_;
}
@property (nonatomic, readonly) BOOL isFinished;
and I want to set it to YES
in a block at some point later, without creating a retain cycle to self
:
- (void)doSomethingWithFinishedBlock:(MyFinishedBlock)theFinishedBlock {
__weak MyClass *weakSelf = self;
MyFinishedBlock finishedBlockWrapper = ^(BOOL success, NSError *e) {
[weakSelf willChangeValueForKey:@"isFinished"];
weakSelf -> isFinished_ = YES;
[weakSelf didChangeValueForKey:@"isFinished"];
theFinishedBlock(success, e);
};
self.finishedBlock = finishedBlockWrapper; // finishedBlock is a class ext. property
}
I'm unsure that this is the right way to do it (I hope I'm not embarrassing myself here ^^). Will this code leak, or break, or is it fine? Perhaps there is an easier way I have overlooked?
SOLUTION
Thanks to the answers below (especially Krzysztof Zablocki), I was shown the way to go here:
Define isFinished
as readwrite
property in the class extension (somehow I missed that one) so no direct ivar assignment is needed, and change code to:
- (void)doSomethingWithFinishedBlock:(MyFinishedBlock)theFinishedBlock {
__weak MyClass *weakSelf = self;
MyFinishedBlock finishedBlockWrapper = ^(BOOL success, NSError *e) {
MyClass *strongSelf = weakSelf;
strongSelf.isFinished = YES;
theFinishedBlock(success, e);
};
self.finishedBlock = finishedBlockWrapper; // finishedBlock is a class ext. property
}
© Stack Overflow or respective owner