Shyam Bhimani Shyam Bhimani - 5 months ago 22
Swift Question

Function error handling in Swift

I am implementing Local Search in my CoreLocation and MapKit frimeworks based app. I am following this Tutorial

I am getting error below error :


Cannot convert value of type '(MKLocalSearchResponse!, NSError!) ->
()' to expected argument type 'MKLocalSearchCompletionHandler' (aka
'(Optional, Optional) -> ()')


Here is my code:

import UIKit
import CoreLocation
import MapKit


class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate{
private let LocationManager = CLLocationManager()
let request = MKLocalSearchRequest()
private var previousPoint:CLLocation?
private var totalMovementDistance:CLLocationDistance = 0




@IBOutlet var mapView: MKMapView!


@IBOutlet var searchText: UITextField!
var matchingItems: [MKMapItem] = [MKMapItem]()

@IBAction func textFieldReturn(sender:AnyObject) {
sender.resignFirstResponder()
mapView.removeAnnotations(mapView.annotations)
self.performSearch()

}

@IBOutlet var latitudeLabel: UILabel!
@IBOutlet var longitudeLabel: UILabel!
@IBOutlet var horizontalAccuracy: UILabel!
@IBOutlet var altitudeLabel: UILabel!
@IBOutlet var verticalAccuracyLabel: UILabel!
@IBOutlet var distanceTraveledLabel: UILabel!






override func viewDidLoad() {
super.viewDidLoad()
LocationManager.delegate = self
LocationManager.desiredAccuracy = kCLLocationAccuracyBest
LocationManager.requestWhenInUseAuthorization()



// Do any additional setup after loading the view, typically from a nib.
}



func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
print("Authorization Status Changed to \(status.rawValue)")
switch status {
case .Authorized, .AuthorizedWhenInUse:
LocationManager.startUpdatingLocation()
mapView.showsUserLocation = true

default:
LocationManager.stopUpdatingLocation()
mapView.showsUserLocation = false
}
}

func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
let errorType = error.code == CLError.Denied.rawValue ? "Access Denied": "Error \(error.code)"
let alertController = UIAlertController(title: "Location Manager Error", message: errorType, preferredStyle: .Alert)
let okAction = UIAlertAction(title: "OK", style: .Cancel, handler: {action in})
alertController.addAction(okAction)
presentViewController(alertController, animated: true, completion: nil)
}

func performSearch() {

matchingItems.removeAll()
let request = MKLocalSearchRequest()
request.naturalLanguageQuery = searchText.text
request.region = mapView.region

let search = MKLocalSearch(request: request)

search.startWithCompletionHandler { (localResponse:MKLocalSearchResponse?, error: NSError?) -> Void in


if error != nil {
print("Error occured in search: \(error?.localizedDescription)")
} else if localResponse == 0 {
print("No matches found")
} else {
print("Matches found")

for item in localResponse.mapItems {
print("Name = \(item.name)")
print("Phone = \(item.phoneNumber)")

self.matchingItems.append(item as MKMapItem)
print("Matching items = \(self.matchingItems.count)")

var annotation = MKPointAnnotation()
annotation.coordinate = item.placemark.coordinate
annotation.title = item.name
self.mapView.addAnnotation(annotation)
}
}
}
}


func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let newLocation = (locations as [CLLocation]) [locations.count - 1]

let latitudeString = String(format: "%g\u{00B0}", newLocation.coordinate.latitude)
latitudeLabel.text = latitudeString

let longitudeString = String(format: "%g\u{00B0}", newLocation.coordinate.longitude)
longitudeLabel.text = longitudeString

let horizontalAccuracyString = String(format: "%gm", newLocation.horizontalAccuracy)
horizontalAccuracy.text = horizontalAccuracyString

let altitudeString = String(format: "%gm", newLocation.altitude)
altitudeLabel.text = altitudeString

let verticalAccuracyString = String(format: "%gm", newLocation.verticalAccuracy)
verticalAccuracyLabel.text = verticalAccuracyString

if newLocation.horizontalAccuracy < 0 {
//invalid accuracy
return

}


if newLocation.horizontalAccuracy > 100 ||
newLocation.verticalAccuracy > 50 {
return
}

if previousPoint == nil {
totalMovementDistance = 0
let start = Place(title:"Strating Point", subtitle:"This is where we started", coordinate:newLocation.coordinate)
mapView.addAnnotation(start)
let region = MKCoordinateRegionMakeWithDistance(newLocation.coordinate, 100, 100)
mapView.setRegion(region, animated:true)


}else {
print("movement distance: \(newLocation.distanceFromLocation(previousPoint!))")
totalMovementDistance += newLocation.distanceFromLocation(previousPoint!)

}

previousPoint = newLocation

let distanceString = String(format: "%gm", totalMovementDistance)
distanceTraveledLabel.text = distanceString



}



}


Error is on this line:

search.startWithCompletionHandler({(response:
MKLocalSearchResponse!,


Edited:

enter image description here

Answer

Replace the ! in the arguments with ? (because that's the signature of the method) Example:

    search.startWithCompletionHandler { (localResponse:MKLocalSearchResponse?, error:NSError?) -> Void in

    }