1

I'm learning iOS development and currently building an iOS app. Below I try to fetch data from Amadeus's Airport and City Search API. There's no data coming back from any keywords that match to any cities in the US or UK.

https://developers.amadeus.com/self-service/category/air/api-doc/airport-and-city-search

static func getAirports(for searchKeywordForCity: String, completion: @escaping (Result<[Airport], NetworkError>) -> Void) {
        
        let endpoint = Endpoint(scheme: .https, host: baseURLForAmadeus, path: pathForAmadeus, queryItems: [
            URLQueryItem(name: typeQueryKey, value: typeQueryValue),
            URLQueryItem(name: searchQueryKey, value: searchKeywordForCity),
            URLQueryItem(name: sortQueryKey, value: sortQueryValue),
            URLQueryItem(name: viewQueryKey, value: viewQueryValue)
        ])
        
        guard let finalURL = endpoint.url else { return completion(.failure(.invalidURL)) }
        print(finalURL)
        var request = URLRequest(url: finalURL)
        request.httpMethod = "GET"
        request.addValue("application/vnd.amadeus+json", forHTTPHeaderField: "content-type")
        request.addValue("Authorization", forHTTPHeaderField: "Bearer \(apiKey)")
        
        let task = URLSession.shared.dataTask(with: request) { data, response, error in
            if let error = error {
                completion(.failure(.thrownError(error)))
                print("Error in \(#function) : \(error.localizedDescription) \n---\n \(error)")
            } else {
                guard let data = data else { return completion(.failure(.noData)) }
                do {
                    let topLevelObject = try JSONDecoder().decode(TopLevelObject.self, from: data)
                    guard let airport = topLevelObject.data else { return completion(.failure(.noData)) }
                    
                    completion(.success(airport))
                } catch {
                    completion(.failure(.serverError(error)))
                    print("Error in \(#function) : \(error.localizedDescription) \n---\n \(error)")
                }
            }
        }
        task.resume()
    }

// Data Model

struct TopLevelObject: Codable { 
    let data: [Airport] 
}

struct Airport: Codable {

    let name, detailedName: String
    let id: String
    let timeZoneOffset, iataCode: String
    let geoCode: GeoCode
    let address: Address
}

struct Address: Codable {
    let cityName, cityCode, countryName, countryCode, regionCode: String
}

struct GeoCode: Codable {
    let latitude, longitude: Double
}

isherwood
  • 58,414
  • 16
  • 114
  • 157
alidinc
  • 51
  • 6
  • 1
    And what's happening exactly in your code? What's the value of `data` for instance? Any logs in console? What parts of your code is called? – Larme Sep 02 '21 at 20:51
  • Hi @Larme, I get this error in the console. ```Error in getAirports(for:completion:) : The data couldn’t be read because it is missing. --- keyNotFound(CodingKeys(stringValue: "name", intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: "No value associated with key CodingKeys(stringValue: \"name\", intValue: nil) (\"name\").", underlyingError: nil))``` I tried making each property optional for data model then data comes as nil – alidinc Sep 02 '21 at 20:54
  • print(String(data: data, encoding: .utf8)) and share it, it might be interesting to show the JSON you are getting... – Larme Sep 02 '21 at 20:56
  • @Larme it's still showing the same error. I called that in do catch block. Is that right? – alidinc Sep 02 '21 at 21:00
  • It to share with us the real response you are getting. It's more debugging than fixing right now... – Larme Sep 02 '21 at 21:01
  • Sadly not getting anything back in the console from printing data :( – alidinc Sep 02 '21 at 21:07
  • It should appear something, else the error would be on "data", not "name"... What's `data.count`? – Larme Sep 02 '21 at 21:17
  • @Larme It doesn't show anything else in the console. Just giving the same error – alidinc Sep 03 '21 at 01:24
  • Where did you put the print? Before the `try JSONDecoder()` I hope, or in the `catch`?. You can see additional why I'm asking you to print the response in the second part of my previous answer: https://stackoverflow.com/a/69002191/1801544 where I suggest to print it in the `catch`. – Larme Sep 03 '21 at 07:06

0 Answers0