swiftcocoaappkitnssearchfield

Why does Xcode tell me 'stringValue' or 'text' cannot be used on type 'NSSearchField'?


I'm actually Swift Developer with main emphasis in UIKit and not AppKit or Cocoa. So Xcode tells me NSSearchField.stringValue? nor NSSearchField.text? is working, is that true? and if so, what should I use instead?

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)
            }
        }

Solution

  • NSSearchField is the class. sender is an instance of NSSearchField.

    You want to use the instance of the class sender.stringValue