2

我已经有了从服务器收到的响应数据。此响应数据有一些面包师数据。

现在我想计算用户和面包店的距离,然后将其存储在同一个模态类中。我为它创建了一个函数。并且由于这个功能需要在 4,5 视图控制器中使用,我的计划是创建作为 UIViewController 的扩展

func getDistanceUserBakery(bakeryData : inout [BakeryRecord], completion : @escaping (Int?) -> () ) {

    for index in 0...(bakeryData.count-1) {
        //1
        let googleApiAdd = "https://maps.googleapis.com/maps/api/distancematrix/json?units=imperial&"
        //2
        let origin = "origins=\(UserLocation.coordinates.latitude),\(UserLocation.coordinates.longitude)"
        //3
        let destination = "&destinations=\(bakeryData[index].location?.coordinates?[1] ?? 0.0),\(bakeryData[index].location?.coordinates?[0] ?? 0.0)"
        //4
        let googleKey = "&key=\(GOOGLE_KEY)"
        //5
        let url = googleApiAdd + origin + destination + googleKey

        let request = URLRequest(url: URL(string: url)!)

        //6 - this line is showing the error.

        let task = URLSession.shared.dataTask(with: request) {(data, response, error) in
            guard let data = data else {
                completion(nil)
                Toast.show(message: "Unable to calculate distance from user to bakery", controller: self)
                return }
            let stringResponse = String(data: data, encoding: .utf8)!
            let dictData = stringResponse.convertToDictionary()
            do {
                let jsonData = try JSONSerialization.data(withJSONObject: dictData as Any, options: .prettyPrinted)
                let decoder = JSONDecoder()
                let model = try decoder.decode(GoogleDistance.self, from: jsonData)
                bakeryData[index].disanceInMiles = model.rows?[0].elements?[0].distance?.text ?? "NaN"
                completion(index)
            } catch let parsingError {
                print("Error data :", parsingError)
                completion(nil)
            }
        }
        task.resume()
    }

这就是我从服务器接收到数据后调用此函数的方式,

  self.getDistanceUserBakery(bakeryData: &self.bakeryData) { index in
                    if index != nil {
                        DispatchQueue.main.async {
                            // here I am thinking as the bakeryData will hold the new value for distanceInMiles, the collectionView will start showing up that result on reload.
                            self.resultCollection.reloadItems(at: [IndexPath(item: index!, section: 0)])
                        }
                    }
                }

现在的问题:

据我所知,当您将参数作为 inout 传递时,可以从函数内部更改值,而这些更改会反映在函数外部的原始值中。

但是当我尝试代码时,它说Escapingclosure captures 'inout' parameter 'bakeryData'。在我的代码中,//6 正在产生错误。

如何修复此错误?

4

1 回答 1

0

正如@Paulw11 在评论中建议的那样,

BakeryData 是一个结构吗?如果是这样,那么只需将其设为一个类。如果您将 BakerData 设为一个类,则该数组包含引用类型,您可以更新元素的属性

我将结构更改为类,它确实有效。

于 2020-01-28T10:11:06.120 回答