为什么 Xcode 告诉我 'stringValue' 或 'text' 不能用于类型 'NSSearchField'?
Why does Xcode tell me 'stringValue' or 'text' cannot be used on type 'NSSearchField'?
我实际上是 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
就是class。 sender
是 NSSearchField
的 实例 。
您想使用 class sender.stringValue
的 实例
我实际上是 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
就是class。 sender
是 NSSearchField
的 实例 。
您想使用 class sender.stringValue