【问题标题】:How to Solve NSArray element failed to match the Swift Array Element type in GooglePlaces API findAutocompletePredictions?如何解决 NSArray 元素无法匹配 GooglePlaces API findAutocompletePredictions 中的 Swift Array 元素类型?
【发布时间】:2020-06-09 03:38:04
【问题描述】:

我无法分析GMSPlacesClient findAutocompletePredictionsFromQuery: 的回调给出的结果,这是一个 GMSAutocompletePrediction 数组。

应用程序将在 let searchResultsWayPoints = finalResults.map 上崩溃, 说明

Precondition failed: NSArray element failed to match the Swift Array Element type
Expected GMSAutocompletePrediction but found GMSAutocompletePrediction
func getGoogleWayPoint(text: String) -> Promise<[GoogleWayPoint]> {
        return Promise<[GoogleWayPoint]> { resolver in
            if text.isEmpty {
                resolver.fulfill([])
                return
            }
            placesClient.findAutocompletePredictions(fromQuery: text, filter: filter, sessionToken: sessionToken) { (results, error) in
                if let error = error {
                    dprint(error.localizedDescription)
                    resolver.fulfill([])
                    return
                }


                guard let finalResults = results else {
                    dprint("can't found results")
                    resolver.fulfill([])
                    return
                }
                let searchResultsWayPoints = finalResults.map {
                    GoogleWayPoint(id: $0.placeID, address: $0.attributedFullText.string)
                }
                resolver.fulfill(searchResultsWayPoints)
            }
        }
    }

任何帮助将不胜感激。谢谢。

【问题讨论】:

标签: google-places-api swift5


【解决方案1】:

所以我已经解决了这个问题,但我没有解决任何问题,因为它来自 GooglePlaces 框架。

为了解决这个问题,只需在回调中将results 设为results:[Any]?

然后,在guard let,安全地将其转换为[GMSAutocompletePrediction]

这是完整的代码

func getGoogleWayPoint(text: String) -> Promise<[GoogleWayPoint]> {
        return Promise<[GoogleWayPoint]> { resolver in
            if text.isEmpty {
                resolver.fulfill([])
                return
            }
            placesClient.findAutocompletePredictions(fromQuery: text, filter: filter, sessionToken: sessionToken) { (results: [Any]?, error) in
                if let error = error {
                    dprint(error.localizedDescription)
                    resolver.fulfill([])
                    return
                }


                guard let finalResults = results as? [GMSAutocompletePrediction] else {
                    dprint("can't found results")
                    resolver.fulfill([])
                    return
                }
                let searchResultsWayPoints = finalResults.map {
                    GoogleWayPoint(id: $0.placeID, address: $0.attributedFullText.string)
                }
                resolver.fulfill(searchResultsWayPoints)
            }
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-18
    • 1970-01-01
    • 2014-11-11
    相关资源
    最近更新 更多