【问题标题】:How to get Objective-C block input parameter in Swift Closure如何在 Swift Closure 中获取 Objective-C 块输入参数
【发布时间】:2015-03-31 08:32:09
【问题描述】:

我需要在我的 Swift 项目中使用 Objective-C SDK,下面是 Objc 演示

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

它将一个块传递给 payOrder:orderString:: 函数,但是当我在 Swift 中调用它时,自动完成帮助我生成这些代码

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

在 Swift 中闭包输入参数没有名称,在 Objc 中它命名为 resultDict,但在 Swift 中我不知道如何获取它的指针,请帮助我,谢谢

【问题讨论】:

    标签: objective-c swift closures block


    【解决方案1】:

    在目标 C 块中,它需要一个 NSDictionary 参数。使用 Swift 闭包,闭包已经类型化,因此您不必将 NSDictionary 声明为类型,您甚至不需要 -> Void, callback: 在 Swift 中也是无关紧要的,因为尾随闭包所以你的最终产品应该是:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-21
      • 1970-01-01
      • 1970-01-01
      • 2016-04-07
      相关资源
      最近更新 更多