swift - 类型 "localSearch"的值没有成员 "start"?

标签 swift error-handling member separator

我在 Xcode 7.3.1 中遇到此问题。我本质上是在 map 上添加一个搜索栏。这是上下文代码的第一部分:

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,我得到:

"Value of type MKLocalSearch has no member start"

我对 Swift 相当陌生,并且对错误是什么感到非常困惑。

我还得到:

"expected separator ",""

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

最佳答案

由于您使用的是旧版本的 swift (swift 2.2),所以我认为您需要

替换

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

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

对于分隔符的错误,请尝试以下操作:

UIAlertActionStyle.Default

而不是

UIAlertActionStyle.default

注意:我认为您还需要将 UIAlertControllerStyle.alert 更改为 UIAlertControllerStyle.Alert

关于swift - 类型 "localSearch"的值没有成员 "start"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46230729/

相关文章:

joomla - fatal error : Call to a member function getKeyName()

c++ - 编译器错误 : 'private' renders variables inaccessible within the class

ios - 卡住 : Analog Clock in Swift iOS

error-handling - 如果超过任务时间限制, celery 可以给我发电子邮件吗?

asp.net-mvc - MVC模型验证

c# - 可观察管道中的异常处理

class - 我无法从 Kotlin 的嵌套类中联系到任何类成员

ios - 从获取请求 Swift 中检索对象

ios - 第一个单元格 UItableViewController 问题 Swift

objective-c - 如何通过 swift 使用 Chartboost 分析?