MKMapItem placemark is unavailable in swift

落花浮王杯 提交于 2019-12-04 11:15:42

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

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

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

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


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

Example:

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



Also, this line after the for loop:
self.userSearch = response.mapItems.placemark

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).

This line should most likely be removed.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!