Invoke block iOS
18,041
Solution 1
You should use copy attribute when you are declaring block property. Like:
@property (nonatomic, copy) id block;
Solution 2
if you want to invoke the block you can simply do this block();
instead of [block invoke];
for more details, see the Block Programming Topics
Solution 3
You have to put the block on the heap:
self.block = Block_copy(^{
[self someMethod];
});
EDIT: @murat's answer is correct, too (and probably better). One way or the other, you have to copy the block, since blocks are actually created on the stack and not on the heap.
For more on blocks you want to keep around, see "Copying Blocks" and "Patterns to Avoid" in the documentation.
Related videos on Youtube
Author by
Matrosov Oleksandr
Updated on June 04, 2022Comments
-
Matrosov Oleksandr almost 2 years
I try to invoke some block, but I run into a EXC_BAD_ACCESS.
-(void) methodA { self.block = ^ { [self methodB]; }; } -(void) webViewDidFinishLoad:(UIWebView *)webView { [block invoke]; // error here (block is not valid id type). } -(void)methodB { //do something }
Any thoughts on why this is happening?
-
Matrosov Oleksandr about 12 yearsI try to call block(); and get error Called object '*(struct objc_object **)((char *)self + OBJC_IVAR_$_SettingsHelp.block)' is not a function
-
murat about 12 yearsthat is because you are declaring it as id. Take a look at this question to see how you can declare it properly.
-
Zaporozhchenko Oleksandr about 7 yearsHow to do same stuff with swift? i mean if i have a closure and i want just call func doneAction() { self.onActionSheetDone() }