为什么 Xcode 告诉我"stringValue"或"text"不能用于类型"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是类。senderNSSearchField实例

您想要使用类sender.stringValue实例

相关内容

  • 没有找到相关文章

最新更新