Skip to content

Instantly share code, notes, and snippets.

@correia
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save correia/61099cda6353441ae32a to your computer and use it in GitHub Desktop.
Save correia/61099cda6353441ae32a to your computer and use it in GitHub Desktop.
Putting a Swift closure in an Objective-C bridgeable dictionary.
// In Objective-C
typedef NSArray * (^ArrayReturningBlock)(void);
id BlockFromClosure(ArrayReturningBlock closure)
{
return [closure copy];
}
void CallBlockFromDictionary(NSDictionary *dictionary)
{
ArrayReturningBlock block = dictionary[@"block"];
NSLog(@"block result = %@", block());
}
// In Swift
import Foundation
let closure = { () -> [AnyObject]! in
let a: [AnyObject] = [1, 2, 3]
return a
}
let dictionary: [String : AnyObject] = [
"block": BlockFromClosure(closure)
]
CallBlockFromDictionary(dictionary)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment