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

时间:2015-01-27 16:05:46

标签: 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

1 个答案:

答案 0 :(得分:1)

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

目标-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  
}]

...夫特

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
        }
    }
}

无论如何都是这样的。