I'm trying to get JSON but it shows an occurred issue Line starting with if let deliveryObject How to get rid of the problem?

code:

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 Answers