"Initializer for conditional binding must have optional type, not 'string'" 但在那种情况下还有什么?
"Initializer for conditional binding must have optional type, not 'string'" but what else in that situation?
“条件绑定的初始化程序必须具有可选类型,而不是 'string'。
我尝试了 sender.stringValue
和 sender.stringValue?
但 Xcode 事情又变得有趣了。 (上面的属性错误消息)
extension MapViewController: NSSearchFieldDelegate {
override func searchFieldAction(sender: NSSearchField) {
// Note: use defer to dismiss the search controller even if the guard returns
defer {
}
guard let text = sender.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)
}
}
你只要
let text = sender.stringValue.stringByTrimmingCharactersInSet(.whitespaceAndNewlineCharacterSet())
guard !text.isEmpty else { return }
“条件绑定的初始化程序必须具有可选类型,而不是 'string'。
我尝试了 sender.stringValue
和 sender.stringValue?
但 Xcode 事情又变得有趣了。 (上面的属性错误消息)
extension MapViewController: NSSearchFieldDelegate {
override func searchFieldAction(sender: NSSearchField) {
// Note: use defer to dismiss the search controller even if the guard returns
defer {
}
guard let text = sender.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)
}
}
你只要
let text = sender.stringValue.stringByTrimmingCharactersInSet(.whitespaceAndNewlineCharacterSet())
guard !text.isEmpty else { return }