I have an NSUInteger defined as a property like so:
@property (nonatomic, assign) NSUInteger wordDisplayed;
I need to have this as a property because I need access to read/write to this variable from other methods and classes.
When trying to change the value of this property within a block, I get the following warning:
Capturing ‘self’ strongly in this block is likely to lead to a retain cycle
“Block will be retained by an object strongly retained by the captured object”
How can I update this variable property within a block?
The block I’m using is in a modified UIActionSheet that I’m using to make the UIActionSheet accept blocks.
https://github.com/zoul/Lambda-Alert
Here is an example of my code:
sectionHeadersAct = [[LambdaSheet alloc] initWithTitle:@"Book 2 Lesson 1"];
[sectionHeadersAct addButtonWithTitle:@"D. E. F. & G. Teach New Letters" block:^{
wordDisplayed = 15; //This is where I'm trying to change the value
}];
[sectionHeadersAct showInView:self.view];
This post deals with this certain issue: https://stackoverflow.com/a/8159567/656036
To summarize it: You should use a weak pointer to self. If you use ARC, you can solve this by writing this before your block:
And using weakSelf instead of self in the block itself.
EDIT Try this: