我目前正在尝试将 Venmo-iOS-SDK 用于我正在开发的应用程序。 SDK 在 objective-C 中,而我正在尝试将它与 swift 应用程序一起使用。
我在将完成 obj-c block 的语法转换为 swift 时遇到问题。我找到了实现我想使用的功能的示例代码。
- (IBAction)logInButtonAction:(id)sender {
[[Venmo sharedInstance] requestPermissions:@[VENPermissionMakePayments,
VENPermissionAccessProfile]
withCompletionHandler:^(BOOL success, NSError *error) {
if (success) {
NSLog("Success")
} else {
NSLog("Failure")
}
}];
}
我试过这样做
@IBAction func loginButtonAction(sender: AnyObject){
Venmo.sharedInstance().requestPermissions([VENPermissionMakePayments, VENPermissionAccessPhone], withCompletionHandler: { (success: Bool, error: NSErrorPointer) -> Void in
if success{
println("Yes")
}else{
println("No")
}
})
}
但是报错
"Cannot invoke 'requestsPermissions with an argument list of type '([String], withCompletionHandler: (Bool, NSError) -> Void)'
这是我翻译 block 的问题吗?或者是其他东西。查看 Venmo-SDK,obj-C 函数定义如下
- (void)requestPermissions:(NSArray *)permissions withCompletionHandler:(VENOAuthCompletionHandler)handler;
和
- (void)requestPermissions:(NSArray *)permissions withCompletionHandler:(VENOAuthCompletionHandler)handler;
最佳答案
你可以这样写(注意完成处理程序参数中缺少类型):
@IBAction func loginButtonAction(sender: AnyObject) {
Venmo.sharedInstance().requestPermissions([VENPermissionMakePayments, VENPermissionAccessPhone], withCompletionHandler: { (success, error) -> Void in
// code here
})
}
使用 Swift 2 语法更简洁一点是省略 -> Void
和显式 withCompletionHandler:
参数:
@IBAction func loginButtonAction(sender: AnyObject) {
Venmo.sharedInstance().requestPermissions([VENPermissionMakePayments, VENPermissionAccessPhone]) { (success, error) in
// code here
}
}
您还需要确保将 println
语句更改为 print
。
关于ios - 带有 Objective-C 函数的 Swift block 语法 [Venmo-iOS-SDK],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34149855/