"localSearch" 类型的值没有成员 "start"?
Value of type "localSearch" has no member "start"?
我在 Xcode 7.3.1 中遇到了这个问题。我本质上是在地图上添加一个搜索栏。这是上下文代码的第一部分:
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
我在 Xcode 7.3.1 中遇到了这个问题。我本质上是在地图上添加一个搜索栏。这是上下文代码的第一部分:
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