swift - 使用 Swift 将位置发布到 iCloud 数据库

标签 swift cllocation

我正在尝试将我的位置发布到 iCloud 数据库。我不断收到“预期声明”错误,我正在尝试修复该错误,但不知道该怎么做。

我的代码如下:

CLGeocoder *geocoder = [CLGeocoder new]

[geocoder geocodeAddressString:artwork[kArtworkAddressKey] completionHandler:^(NSArray *placemark, NSError *error){
    if (!error) {
        if (placemark.count > 0) {
            CLPlacemark *placement = placemark[0]
            artworkRecord[kArtworkLocationKey] = placement.location
        }   
    } else {
        // insert error handling here
    }
    // Save the record to the database  
}]

我是新手,如果这是一个简单的问题,请原谅。

enter image description here

最佳答案

问题是您已将 Objective-C 放入 Swift 文件中。你可以这样翻译...

objective-c

CLGeocoder *geocoder = [CLGeocoder new]

[geocoder geocodeAddressString:artwork[kArtworkAddressKey] completionHandler:^(NSArray *placemark, NSError *error){
    if (!error) {
        if (placemark.count > 0) {
            CLPlacemark *placement = placemark[0]
            artworkRecord[kArtworkLocationKey] = placement.location
        }   
    } else {
        // insert error handling here
    }
    // Save the record to the database  
}]

swift ...

let geocode = CLGeocoder()

geocoder.geocodeAddressString(artwork[kArtworkAddressKey]) {
    placemark, error in
    if error {
        // handle error
    } else {
        if let placement = placemark[0] {
            self.artworkRecord[kArtWorkLocationKey] = placement.location
        }
    }
}

无论如何,类似的事情。

关于swift - 使用 Swift 将位置发布到 iCloud 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28174818/

相关文章:

ios - 我正在尝试在 Swift 中创建两点之间的路线,但我的 MKDirectionsResponse 似乎出现错误

objective-c - 将小数坐标转换成度、分、秒、方向

ios - 为什么将 CLGeocoder 与 "placemarks"数组一起使用?

swift - makeNode 中的上下文是什么(在 : Context) vapor 3?

arrays - 如何在 Swift 中返回数组的前 5 个对象?

swift - 在 Swift 的链式方法中将调用对象作为参数传递

ios - RestKit RKObjectMapping 到 CLLocation

swift - 计算两个位置之间的方位

swift - 在第一次启动时显示另一个 View Controller ,而不是再次

iOS – 如何让 UIView 捕获并转发触摸事件?