Search code examples
objective-cswiftclosuresblock

How to get Objective-C block input parameter in Swift Closure


I need use a Objective-C SDK in my Swift project, the Objc demo is in below

[[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic) {
   NSLog(@"reslut = %@",resultDic);
}];

It pass a block to payOrder:orderString:: function, but when I call it in Swift, the auto complete help me generate these code

AlipaySDK.defaultService().payOrder(orderString, fromScheme: self.aliAppScheme, callback: { ([NSObject : AnyObject]!) -> Void in
    println("Pay Success")
})

in Swift the closure input parameter has no name, in Objc it named resultDict, but in Swift I don't know how to get it pointer, Please help me, Thanks


Solution

  • In the objective C block, it takes an NSDictionary parameter. With Swift closures, the closure is already typed so you don't have to declare NSDictionary as the type and you really don't even need -> Void. Also the , callback: is extraneous in Swift as well because of trailing closures so your final product should be:

    AlipaySDK.defaultService().payOrder(orderString, fromScheme: self.aliAppScheme) { resultDict in
        println("Pay Success")
    }