我在 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/