-1

When I do println(localSearchResponse), I get a MapItem object, which includes a ton of details about the location. In this example, its UCSD. Here is the output showing in my log.

    <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}"
);
}

Notice how it outputs placemark = University of California... and has the address? How do I get this value and store it into a variable? I tried localSearchResponse.mapItems.first.placemark and it generated an error: "error: 'placemark' is unavailable: APIs deprecated as of ios7 and earlier are unavailable in Swift"

Here is my code:

    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



    }

Here is the documentation of MKSearchResponse

And here is the documentation of MKMapItem

Josh O'Connor
  • 4,694
  • 7
  • 54
  • 98

1 Answers1

1

The error is misleading. Nothing here has been deprecated. You need to handle some optionals and a typecast:

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
}
vacawama
  • 150,663
  • 30
  • 266
  • 294
  • @JoshOConnor, did that solve your problem? Please accept my answer if it did, or post your own answer if you solved it another way yourself. – vacawama Jul 22 '15 at 10:13
  • http://stackoverflow.com/questions/31488686/accessing-mklocalsearchresponse-item-swift – Josh O'Connor Jul 23 '15 at 07:05