When writing a method that accepts a block as an argument, do I need to do anything special such as copying the block to the heap before executing it? For example, if I had
This all looks good. Though, you might want to double-check the block parameter:
@property id myObject;
@property (copy) void (^myBlock)(NSString *);
....
- (void)testWithBlock: (void (^)(NSString *))block
{
NSString *testString = @"Test";
if (block)
{
block(test);
myObject = Block_copy(block);
myBlock = block;
}
}
...
[object testWithBlock: ^(NSString *test)
{
NSLog(@"[%@]", test);
}];
Should be fine. And I believe that they are even trying to phase out Block_copy()
, but they haven't yet.