ipad app development: Block_copy not working
I am new to this site, I hope you can help.
I get an EXC_BAD_ACCESS error from the following code in function():
typedef int (^BlockDef)();
BlockDef getBlockDef(int d)
{
BlockDef def = Block_copy(^()
{
return d;
});
return def;
}
void function()
{
BlockDef def1 = getBlockDef(1);
BlockDef def2 = getBlockDef(2);
printf("%d\n",def1());
printf("%d\n",def2());
NSMutableArray * arr = [NSMutableArray array];
[arr addObject:def1];
[arr addObject:def2];
printf("%d\n",((BlockDef) [arr objectAtIndex:0])()); // **** EXC_BAD_ACCESS here
printf("%d\n",((BlockDef) [arr objectAtIndex:1])());
}
This code ru开发者_JS百科ns without any problem in iphone, but not in ipad. What may be the problem?
Also, in ipad, when I remove the Block_copy call, it runs through the same line successfully. This is weird since a non copied function sould not be returned or should not be called (In iphone, I get an EXC_BAD_ACCESS at first printf's in this situation).
Thank you for your assistance.
When you say iPad, do you mean 3.2? If so this won't work. There is a half finished blocks API in 3.2 that will let you do everything, except copy blocks.
If your iPad is on 4.2 and this still doesn't work, let me know.
精彩评论