Search code examples
swifterror-handlingmemberseparator

Value of type "localSearch" has no member "start"?


I'm having this issue in Xcode 7.3.1. I'm essentially adding a search bar to a map. Here's the first part of the code for context:

import UIKit
import MapKit

class FirstViewController: UIViewController, UISearchBarDelegate {

var searchController:UISearchController!
var annotation:MKAnnotation!
var localSearchRequest:MKLocalSearchRequest!
var localSearch:MKLocalSearch!
var localSearchResponse:MKLocalSearchResponse!
var error:NSError!
var pointAnnotation:MKPointAnnotation!
var pinAnnotationView:MKPinAnnotationView!

@IBAction func showSearchBar(sender: AnyObject) {
    searchController = UISearchController(searchResultsController: nil)
    searchController.hidesNavigationBarDuringPresentation = false
    self.searchController.searchBar.delegate = self
    presentViewController(searchController, animated: true, completion: nil)


}

@IBOutlet var mapView: MKMapView!

override func viewDidLoad() {


    super.viewDidLoad()

    let initialLocation = CLLocation(latitude: 47.6062, longitude: -122.3321)

    let regionRadius: CLLocationDistance = 1000
    func centerMapOnLocation(location: CLLocation) {
        let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate,
                                                                  regionRadius * 2.0, regionRadius * 2.0)
        mapView.setRegion(coordinateRegion, animated: true)
    }

    centerMapOnLocation(initialLocation)


}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

Here's the snippet where the error occurs:

func searchBarSearchButtonClicked(searchBar: UISearchBar){
    searchBar.resignFirstResponder()
    dismissViewControllerAnimated(true, completion: nil)
    if self.mapView.annotations.count != 0{
        annotation = self.mapView.annotations[0]
        self.mapView.removeAnnotation(annotation)
    }
    localSearchRequest = MKLocalSearchRequest()
    localSearchRequest.naturalLanguageQuery = searchBar.text
    localSearch = MKLocalSearch(request: localSearchRequest)
    localSearch.start{ (localSearchResponse, error) -> Void in
        if localSearchResponse == nil{
            let alertController = UIAlertController(title: nil, message: "Place Not Found", preferredStyle: UIAlertControllerStyle.alert)
            alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.default, handler: nil))
            self.present(alertController, animated: true, completion: nil)
            return

At localSearch.start, I am getting:

"Value of type MKLocalSearch has no member start"

I'm fairly new to Swift and pretty confused as to what the error is.

I also get:

"expected separator ",""

in the 3rd to last line. But even when changing it, error continues to occur. Could the two be related?


Solution

  • Since you are using older version of swift (swift 2.2) so I think you need to

    replace

    localSearch.start { ...     //this is for swift 3
    

    with

    localSearch.startWithCompletionHandler { ...   //this is for swift 2.2
    

    For the error , of separator try this:

    UIAlertActionStyle.Default
    

    instead of

    UIAlertActionStyle.default
    

    Note : I think you also need to change UIAlertControllerStyle.alert to UIAlertControllerStyle.Alert