MKLocalSearchResponse中的MKMapItem地标已弃用(swift)

时间:2015-07-18 21:49:24

标签: ios swift

当我println(localSearchResponse)时,我得到一个MapItem对象,其中包含大量有关该位置的详细信息。在这个例子中,它的UCSD。这是我日志中显示的输出。

    <MKLocalSearchResponse: 0x1c53d640> {
boundingRegion = "<center:+32.87514836, -117.23958822 span:+0.00725621, +0.00825332>";
mapItems =     (
    "<MKMapItem: 0x1c538090> {\n    isCurrentLocation = 0;\n    name = \"University of California, San Diego\";\n    phoneNumber = \"+18585342230\";\n    placemark = \"University of California, San Diego, 9500 Gilman Dr, La Jolla, CA  92093-5004, United States @ <+32.87529400,-117.23961000> +/- 0.00m, region CLCircularRegion (identifier:'<+32.87514837,-117.23958825> radius 557.57', center:<+32.87514837,-117.23958825>, radius:557.57m)\";\n    url = \"http://www.ucsd.edu\";\n}"
);
}

注意它如何输出placemark = University of California...并具有地址?如何获取此值并将其存储到变量中?我尝试了localSearchResponse.mapItems.first.placemark,它产生了一个错误:&#34;错误:&#39; placemark&#39;不可用:从ios7及更早版本开始使用的API在Swift&#34;中不可用

这是我的代码:

    localSearchRequest = MKLocalSearchRequest()
    localSearchRequest.naturalLanguageQuery = addressTextField.text
    localSearch = MKLocalSearch(request: localSearchRequest)
    localSearch.startWithCompletionHandler { (localSearchResponse, error) -> Void in

        if localSearchResponse == nil{
            var alert = UIAlertView(title: nil, message: "Place not found", delegate: self, cancelButtonTitle: "Try again")
            alert.show()
            return
        }

        //prints the MKLocalSearchResponse with name, phoneNumber, placemark
        println(localSearchResponse)

        //Get latitude and longitude
        var newRecordLat = localSearchResponse.boundingRegion.center.latitude
        var newRecordLong = localSearchResponse.boundingRegion.center.longitude

        //How do I get the address, which is "placemark" in the MKLocalSearchResponse?
        var newRecordAddress = localSearchResponse.mapItems...???


        //store values to Parse
        self.latToParse = newRecordLat
        self.longToParse = newRecordLong



    }

以下是MKSearchResponse

的文档

以下是MKMapItem

的文档

1 个答案:

答案 0 :(得分:1)

该错误具有误导性。这里没有任何东西被弃用了。你需要处理一些选项和类型转换:

Swift 1.2

if let newRecordAddress = (localSearchResponse.mapItems.first as? MKMapItem)?.placemark {
    // use newRecordAddress
}

Swift 2.0

if let newRecordAddress = localSearchResponse.mapItems.first?.placemark {
    // use newRecordAddress
}