代码之家  ›  专栏  ›  技术社区  ›  Aleks

“localSearch”类型的值没有成员“start”?

  •  0
  • Aleks  · 技术社区  · 7 年前

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

    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
    

    localSearch.start ,我得到:

    我对斯威夫特很陌生,对错误是什么感到困惑。

    我还得到:

    “预期分隔符”,“”

    在倒数第三行。但即使在更改它时,错误仍会继续发生。这两者有联系吗?

    1 回复  |  直到 7 年前
        1
  •  0
  •   3stud1ant3    7 年前

    因为您使用的是旧版本的 swift

    代替

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

    具有

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

    对于错误 ,

    UIAlertActionStyle.Default
    

    而不是

    UIAlertActionStyle.default
    

    UIAlertControllerStyle.alert UIAlertControllerStyle.Alert