ios - 将 Objective-C block 转换为 Swift

标签 ios objective-c swift ios8

我只是想将这个 Objective-C block 转换为 swift,但似乎有一个问题,我无法解决。没有一个变量是可选的。

Objective-C - 有效

[CLPlacemark hnk_placemarkFromGooglePlace:place
            apiKey:YOUR_API_KEY
            completion:^(CLPlacemark *placemark, NSString *addressString, NSError *error) {           
        }];

Swift - 给出错误

CLPlacemark.hnk_placemarkFromGooglePlace(placeAtIndexPath(indexPath),"YOUR_API_KEY",
completion:{ (placemark:CLPlacemark!, addressString: NSString!, error: NSError!) -> Void in

            })

错误消息:

Cannot invoke 'hnk_placemarkFromGooglePlace' with an argument list of type '(HNKGooglePlacesAutocompletePlace!, String, completion: (CLPlacemark!, NSString!, NSError!) -> Void)'

Swift 方法签名

 CLPlacemark.hnk_placemarkFromGooglePlace(place:
 HNKGooglePlacesAutocompletePlace!, apiKey:String!, completion:
 ((CLPlacemark!, String!, NSError!) -> Void)

最佳答案

Swift 在 block 中将 NSString 桥接到 String。您仍然在回调中使用 NSString,但应该使用 String。

关于ios - 将 Objective-C block 转换为 Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31147241/

相关文章:

iphone - 如何通过 NSURLConnection sendAsynchronousRequest 取消异步调用?

swift - 创建透明渐变并将其用作 SpriteKit 中的 alpha 蒙版

ios - 如何检查是否按下了 AVPlayerViewController 快进控件?

ios - 将多个二进制文件同时上传到 itunesconnect

objective-c - 在 Objective-C 中 @synchronized 如何锁定/解锁?

iphone - 约束问题(scrollview在iPhone7和7+中不出现,但在SE中出现)

ios - removeFromSuperview() 并不总是有效

ios - 使用 iOS 应用程序的 Objective-C 创建 MS Excel 类型的电子表格

iphone - 如何在 Cocoa Touch 中保存网站并将其存储在数组中?

iphone - UIImagePickerController 检查用户是否编辑了图像