Last active
August 29, 2015 14:27
-
-
Save correia/61099cda6353441ae32a to your computer and use it in GitHub Desktop.
Putting a Swift closure in an Objective-C bridgeable dictionary.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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