pinkman
pinkman

Reputation: 23

ObjC delegate callback in Swift

In ObjC, I've got a function declared as

-(void)fubar:(void(^)(NSDictionary *))callback;

This is how I will use in ObjC

fubar(^(NSDictionary *dict) {
   console.log(dict);
})

How do I use the same in Swift after bridging?

Upvotes: 2

Views: 74

Answers (1)

David East
David East

Reputation: 32604

Since it's the last parameter it can be used as a trailing closure:

fubar { (dict) in
  print(dict) // prints dictionary 
}

Or even shorter

fubar() { 
  print($0) // prints dictionary
}

Read the section on Trailing Closures in Apple's Swift book for more info.

Upvotes: 1

Related Questions