0

我想用来MKLocalSearch从用户在文本字段中键入的文本中获取地理位置结果。由于文本经常更新,我在输入几个字符后最终得到这个错误:

操作无法完成。(MKErrorDomain 错误 3。)

这是我的代码:

class MyViewController: UIViewController, MKLocalSearchCompleterDelegate {
    @IBOutlet weak var textField: UITextField!

    var completer = MKLocalSearchCompleter()
    var results: Set<MKMapItem> = []

    // Called when text field text is updated
    @objc func didUpdateText() {
        results = []
        completer.queryFragment = textField.text ?? ""
    }

    func completerDidUpdateResults(_ completer: MKLocalSearchCompleter) {
        completer.results.forEach { result in
            MKLocalSearch(request: MKLocalSearch.Request(completion: result)).start { result, error in
                // here I handle the MKLocalSearchResponse to extract the data I need
                // this is where I often get *The operation couldn’t be completed* errors
            }
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        textField.addTarget(self, action: #selector(didUpdateText), for: .editingChanged)
    }
}

我应该怎么做才能避免这种情况?

谢谢您的帮助

4

0 回答 0