Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.0k views
in Technique[技术] by (71.8m points)

API not displaying data in Swift for iOS

Currently not getting data from API to display.

Current code:

import SwiftUI

struct Response: Decodable {
    var content: [Result]
}

struct Result : Decodable {
    var code: String
    var fire: String
    var name: String
    var police: String
    var medical: String
}

struct ContentView: View {
    @State private var content = [Result]()

    var body: some View {
        List(content, id: .code) { item in
            VStack(alignment: .leading) {
                Text(item.name)
                    .font(.headline)
                Text(item.medical)
            }
        }
        .onAppear(perform: loadData)
    }
    func loadData() {
        
        let url = URL(string: "https://emergency-phone-numbers.herokuapp.com/country/gb")!

        URLSession.shared.dataTask(with: url) { data, response, error in
            if let error = error { print(error); return }
            do {
                let decodedResponse = try JSONDecoder().decode(Response.self, from: data!)
                // we have good data – go back to the main thread
                DispatchQueue.main.async {
                    // update our UI
                    self.content = decodedResponse.content
                }
                
            } catch {
                print(error)
            }
        }.resume()
        
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Current error:

2021-01-21 19:21:07.094582+0000 iTunes API[39924:1098972] [] nw_protocol_get_quic_image_block_invoke dlopen libquic failed keyNotFound(CodingKeys(stringValue: "content", intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: "No value associated with key CodingKeys(stringValue: "content", intValue: nil) ("content").", underlyingError: nil))

Please advise. Thanks in advance.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Please note that the content from https://emergency-phone-numbers.herokuapp.com/country/gb is:

{"code":"GB","fire":"999","police":"999","name":"United Kingdom","medical":"999"}

The problem is that there's no content on the response that you're getting, so the JSONDecoder is failing to parse the response. It seems the response you get contains content that fits on Result, so if you change your code to parse that, it works fine:

import SwiftUI

struct Response: Decodable {
    var content: [Result]
}

struct Result : Decodable {
    var code: String
    var fire: String
    var name: String
    var police: String
    var medical: String
}

struct ContentView: View {
    @State private var content = [Result]()

    var body: some View {
        List(content, id: .code) { item in
            VStack(alignment: .leading) {
                Text(item.name)
                    .font(.headline)
                Text(item.medical)
            }
        }
        .onAppear(perform: loadData)
    }
    func loadData() {
        
        let url = URL(string: "https://emergency-phone-numbers.herokuapp.com/country/gb")!

        URLSession.shared.dataTask(with: url) { data, response, error in
            if let error = error { print(error); return }
            do {
                let decodedResponse = try JSONDecoder().decode(Result.self, from: data!)
                // we have good data – go back to the main thread
                DispatchQueue.main.async {
                    // update our UI
                    self.content = [decodedResponse]
                }
                
            } catch {
                print(error)
            }
        }.resume()
        
    }
}

I guess, in the end, you probably wanted a collection of Results to fit on the content, so you might need to change something on that API of yours to return that collection, then your code would work fine.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...