【问题标题】:Swift Block Syntax with Objective-C Function [Venmo-iOS-SDK]带有 Objective-C 函数的 Swift 块语法 [Venmo-iOS-SDK]
【发布时间】:2016-03-13 00:24:28
【问题描述】:

我目前正在尝试将 Venmo-iOS-SDK 用于我正在开发的应用程序。 SDK 在 Objective-C 中,而我正在尝试将它与 swift 应用程序一起使用。

我在将完成 obj-c 块的语法转换为 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")
        }
    })
}

但得到错误

"不能使用类型参数列表调用 'requestsPermissions '([String], withCompletionHandler: (Bool, NSError) -> Void)'

这是我如何翻译块的问题吗?或者是其他东西。查看 Venmo-SDK,obj-C 函数是这样定义的

- (void)requestPermissions:(NSArray *)permissions withCompletionHandler:(VENOAuthCompletionHandler)handler;

- (void)requestPermissions:(NSArray *)permissions withCompletionHandler:(VENOAuthCompletionHandler)handler;

【问题讨论】:

    标签: ios swift objective-c-blocks venmo


    【解决方案1】:

    你可以这样写(注意完成处理程序参数上缺少类型):

    @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

    【讨论】:

      猜你喜欢
      • 2015-10-27
      • 1970-01-01
      • 2015-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多