MKMapItem地标在swift中不可用 [英] MKMapItem placemark is unavailable in swift

查看:331
本文介绍了MKMapItem地标在swift中不可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试创建一个搜索栏,以用户类型自动填充位置。我正在使用MKLocalSearch获取MKLocalSearchResponse,它似乎返回我可以使用的值。但是,要从搜索中获取名称,地址或坐标,需要访问搜索响应中的MKPlacemark属性。当我访问地标时,我收到错误:

I am currently trying to make a searchbar that autopopulates with locations as the user types. I am using a MKLocalSearch to get a MKLocalSearchResponse and it appears to return values I can use. However to get the name, address or coordinates out of the search needs access to the MKPlacemark property in the search response. When I access the placemark I get the error:

'placemark'不可用:从iOS7及以前版本中弃用的API在Swift中不可用

'placemark' is unavailable: APIs deprecated as of iOS7 and earlier are unavailable in Swift

var request = MKLocalSearchRequest()
    request.naturalLanguageQuery = searchText
    //PUT HERE: check if network is on?
    let localSearch: MKLocalSearch = MKLocalSearch(request: request)
    localSearch.startWithCompletionHandler { (response: MKLocalSearchResponse!, error: NSError!) -> Void in
        if (error == nil) {
            println("searched")
            for res in response.mapItems {
                self.userSearch.append(res.placemark)
            }
            self.userSearch = response.mapItems.placemark
            self.tableView?.reloadData()
        } else {
            println(error)
        }
    }
}

有没有人知道访问地标的解决方法?

Does anyone know a workaround to accessing the placemark?

谢谢!

推荐答案

response.mapItems 数组在API中声明,类型为 [AnyObject]!

The response.mapItems array is declared in the API as of type [AnyObject]!.

for 循环没有明确说明 res 类型为 MKMapItem (或 response.mapItems 实际上是 [MKMapItem] )。

The for loop isn't explicitly saying that res is of type MKMapItem (or that response.mapItems is actually [MKMapItem]).

所以 res 被视为 AnyObject 的一个实例,它没有被定义为具有地标属性。

So res is treated like an instance of AnyObject which isn't defined as having a placemark property.

这就是为什么你得到编译器错误'placemark'不可用...

This is why you get the compiler error 'placemark' is unavailable....



要解决此问题,请将 res 转换为 MKMapItem 然后地标属性将变为可见。


To fix this, cast res as an MKMapItem and then the placemark property will become visible.

示例:

for res in response.mapItems {
    if let mi = res as? MKMapItem {
        self.userSearch.append(mi.placemark)
    }
}




此外,之后循环的这一行:

self.userSearch = response.mapItems.placemark

不如果 userSearch 应该是一个地标数组,那就没有意义。

loop将地标附加到该数组,然后该行将数组设置为单个地标对象(此外, mapItems 对象甚至没有地标属性)。

doesn't make sense if userSearch is supposed to be an array of placemarks.
The for loop is appending placemarks to that array and then this line is setting the array to a single placemark object (in addition, the mapItems object doesn't even have a placemark property).

此行最有可能被删除。

这篇关于MKMapItem地标在swift中不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆