Here is the Objective-C block:
@property (copy) void (^anObjcBlock)();
anObjcBlock = ^{
NSLog(@"Yea man this thing works!!");
};
NSMutableArray *theArrayThatHasTheBlockInItAtIndexZero = [NSMutableArray array];
[theArrayThatHasTheBlockInItAtIndexZero addObject:anObjBlock];
Here's what I did in Swift:
var theBlock: (()->Void)?
theBlock = theArrayThatHasTheBlockInItAtIndexZero[0] as? ()->Void
// Now call the block
theBlock!()
But with this I get runtime error.
Basically, the theBlock = theArrayThatHasTheBlockInItAtIndexZero[0] as? ()->Void
statement would make theBlock
nil because the as?
failed. And when I changed the statement to theBlock = theArrayThatHasTheBlockInItAtIndexZero[0] as! ()->Void
, I get a runtime error:
I'm not sure what else to do. This is an empty project, there really is no code in it.
var block : (@convention(block) () -> Void)?
. (Swift 2.0 - for Swift 1.x use@objc_block
instead of@convention(block
)