ios – Why is not my inside API construction executing efficiently in SwiftUI?

Spread the love


You’ve gotten a couple of errors in your code.

The primary error happens since you wish to decode Parteien.self however you do not have a Parteien construction.

struct Parteien: Decodable {
    let information: [Partei]
}

However, you do not really want that construction or to fetch the events, as a result of the celebration is within the Politician construction already.

The second error happens as a result of you will have declared a property partei in your PoliticianDetailView struct, however you are not passing in a price. Once more, you do not want that property as a result of it’s already within the Politician.

As an alternative of utilizing a Listing you possibly can simply use an extra Textual content.

struct PoliticianDetailView: View {
    let politician: Politician

    var physique: some View {
        VStack {
            Textual content("Identify: (politician.firstName) (politician.lastName)")
            Textual content("Geburtsjahr (politician.birthYear ?? 0)")
            Textual content("Alter: (calculateAge(birthYear: politician.birthYear ?? 0)) Jahre")
            Textual content("Beschäftigt als (politician.beruf ?? "Unbekannt")")
            Textual content("Geschlecht: (politician.geschlecht ?? "Unbekannt")")

            Textual content("Occasion: (politician.partei.namePartei)")
        }
        .navigationTitle("(politician.firstName) (politician.lastName)")
    }

    func calculateAge(birthYear: Int) -> Int {
        let currentYear = Calendar.present.part(.yr, from: Date())
        return currentYear - birthYear
    }
}

Lastly, you’ll get a runtime error as a result of your CodingKeys for Partei would not map the celebration property out of your JSON to partei in your construction:

struct Politician: Decodable {
    let id: Int
    let firstName: String
    let lastName: String
    let birthYear: Int?
    let beruf: String?
    let geschlecht: String?
    let partei: Partei
    
    enum CodingKeys: String, CodingKey {
        case id
        case firstName = "first_name"
        case lastName = "last_name"
        case birthYear = "year_of_birth"
        case beruf = "occupation"
        case geschlecht = "intercourse"
        case partei = "celebration"
    }
}

Additionally, you possibly can tweak your age calculation in order that it would not present individuals with out a delivery yr as being 2023 years previous

struct PoliticianDetailView: View {
    let politician: Politician
    
    var physique: some View {
        VStack {
            Textual content("Identify: (politician.firstName) (politician.lastName)")
            Textual content("Geburtsjahr (politician.birthYear ?? 0)")
            Textual content("Alter: (calculateAge(birthYear: politician.birthYear))")
            Textual content("Beschäftigt als (politician.beruf ?? "Unbekannt")")
            Textual content("Geschlecht: (politician.geschlecht ?? "Unbekannt")")
            
            Textual content("Occasion: (politician.partei.namePartei)")
        }
        .navigationTitle("(politician.firstName) (politician.lastName)")
    }
    
    func calculateAge(birthYear: Int?) -> String {
        guard let birthYear else {
            return "---"
        }
        let currentYear = Calendar.present.part(.yr, from: Date())
        return String(currentYear - birthYear)+" jahre"
    }
}

And add a sorting by final identify:

actor PoliticianDataService {
    func fetchPoliticians() async throws -> [Politician] {
        guard let url = URL(string: "https://www.abgeordnetenwatch.de/api/v2/politicians") else { return [] }
        
        let (information, _) = strive await URLSession.shared.information(from: url)
        
        let decoder = JSONDecoder()
        let politicians = strive decoder.decode(Politicians.self, from: information)
        return politicians.information.sorted {
            $0.lastName < $1.lastName
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *