我实际上是Swift开发人员,主要关注UIKit,而不是AppKit或Cocoa。所以Xcode告诉我NSSearchField.stringValue?也不是NSSearchField.text?正在工作,是真的吗?如果是,我应该使用什么?
extension MapViewController: NSSearchFieldDelegate {
override func searchFieldAction(sender: NSSearchField)
defer {
}
guard let text = NSSearchField.stringValue?.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())
where !text.isEmpty else { return }
let request = MKLocalSearchRequest()
request.naturalLanguageQuery = text
request.region = mapView.region
let search = MKLocalSearch(request: request)
search.startWithCompletionHandler { (response, error) in
guard let item = response?.mapItems.first else { return }
let annotation = item.placemark
self.mapView.addAnnotation(annotation)
self.mapView.showAnnotations([annotation], animated: true)
self.mapView.selectAnnotation(annotation, animated: true)
}
}
NSSearchField
是类。sender
是NSSearchField
的实例。
您想要使用类sender.stringValue
的实例