Home > Net >  Extracting single String/Int from fetched API data list - SwiftUI
Extracting single String/Int from fetched API data list - SwiftUI

Time:09-17

I am struggling to extract single values (strings and Int) from the data I fetched from an API. I fetch the data in the form of a list:

class apiCall {
    func getRockets(completion:@escaping ([Rockets]) -> ()) {
        guard let url = URL(string: "https://api.spacexdata.com/v4/rockets") else { return }
        URLSession.shared.dataTask(with: url) { (data, _, _) in
            let rockets = try! JSONDecoder().decode([Rockets].self, from: data!)
            print(rockets)
            
            DispatchQueue.main.async {
                completion(rockets)
            }
        }
        .resume()
    }
}

Then when I try using it in a View I succeed when using a List to view the values, like it shows the list of the names from the API data. But then when I want to view a single value like this:

import SwiftUI

struct RocketStatistics: View {
    
    @State var rockets: [Rockets] = []
    
    var body: some View {
        VStack{
            Text(rockets[1].name)
        }
        .onAppear {
            apiCall().getRockets { (rockets) in
                self.rockets = rockets
                    
            }
        }
    }
}
struct RocketStatistics_Previews: PreviewProvider {
    static var previews: some View {
        RocketStatistics()
    }
}

It does not even give me an error, but my preview just won't update and keeps crashing. So my question is how can I extract single values from this API data in List form and use these single values in my whole project?

To keep it simple and make it work first I started just with fetching the "name" from the API:

import Foundation
import SwiftUI

struct Rockets: Codable, Identifiable {
    let id = UUID()
    let name : String
    
}


When it all works I would also want to use Integer values from the API in my project, so tips on how to that are also welcome!

CodePudding user response:

Never ever get items by a hard-coded index in a SwiftUI view without any check. When the view is rendered the first time the array is empty and any index subscription will crash.

Always check if the array contains the required number of items. In this case the array must contain at least two items

VStack{
    Text(rockets.count > 1 ? rockets[1].name : "")
}
  • Related