无法将类型'((key:String,value:Any)')的值转换为预期的参数类型'[String:Any]

时间:2019-05-13 09:26:40

标签: swift

我正在尝试获取JSON,但它显示了一个已发生的问题 以if let deliveryObject开头的行 如何摆脱这个问题?

代码:

struct Tracking {
    let receiver: String
    let placeOfMail: String
    let indexOfMail: Double

    init(json:[String:Any]) throws {
        guard let receiver = json["name"] as? String else {
            throw SerializationError.missing("Receiver data has been missed")
        }
        guard let placeOfMail = json ["address"] as? String else {
            throw SerializationError.missing("Place of delivery has been missed")
        }
        guard let indexOfMail = json ["postindex"] as? Double else {
            throw SerializationError.missing("Index of postmail has been missed")
        }
        self.receiver = receiver
        self.placeOfMail = placeOfMail
        self.indexOfMail = indexOfMail
    }
    static let basePath = "https://track.kazpost.kz/api/v2/"

    static func deliveryData (withTrackid trackid:String, completion: @escaping ([Tracking]) -> ()){

        let url = basePath + trackid
        let request = URLRequest(url: URL(string: url)!)

        let task = URLSession.shared.dataTask(with: request) { (data: Data?, response : URLResponse?, error: Error?) in

            var deliveryArray: [Tracking] = []

            if let data = data {

                do {
                    if let json = try JSONSerialization.jsonObject(with: data, options:[]) as? [String:Any] {
                        if let deliveryInformation = json ["delivery"] as? [String:Any] {
                            if let deliveryPlace = deliveryInformation ["address"] as? [String:Any] {
                                for dataPoint in deliveryPlace {
                                    if let dataPointValue = dataPoint.value as? [String: AnyObject],
                                        let deliveryObject = try Tracking(json: dataPointValue) {
                                        deliveryArray.append(deliveryObject)
                                    }
                                }
                            }
                        }
                    }
                }catch {
                    print(error.localizedDescription)
                }

                completion(deliveryArray)
            }
        }
        task.resume()
    }
}

0 个答案:

没有答案