ios – I attempted to make a easy HTTP request in SwiftUI however once I assign worth to Printed Variable I bought nil


This is the code

import Mix
import Basis


struct Cate: Hashable, Codable {
    var identify: String
    var property: String
}

last class CatesDataModel: ObservableObject {
    
    @Printed var cates_list = [Cate]()
    @Printed var grouped_cates = [GroupedCates]()
    
    
   init() {
        fetchCates(user_id: "X45045")
   }
    
    func fetchCates(user_id: String) {
        
        
        
        let url = URL(string: API_URL)!
        var request = URLRequest(url: url)
        request.httpMethod = "GET"
        

        URLSession.shared.dataTask(with: request) { [weak self] information, response, error in
            
            DispatchQueue.major.async {
                if error != nil || (response as! HTTPURLResponse).statusCode != 200 {
                    
                } else if let information = information {
                    
                    do {
                       
                        let response = strive JSONDecoder().decode(fetchCatesResponse.self, from: information)
                        
                        let grouped_dict = Dictionary(grouping: response.information, by: { $0.property })
                        
                        if response.standing == "success" {
                            self?.cates_list = response.information
                          
                            
                        }
                    } catch {
                        print(error)
                    }
                    
                }
            }
            
        }.resume()
        
    }
    
}

fileprivate struct fetchCatesResponse: Decodable {

    // MARK: - Properties
    
    let standing: String
    let information: [Cate]

}

I do not know the place is flawed, once I execute print(response.information), its worth confirmed accurately, however once I execute print(self?.cates_list), it bought nil?
I am new to SwiftUI from Python Backend, possibly the query is just too easy however I can’t cope with it by myself, hope somebody can inform me the place is flawed.

Similar Posts

Leave a Reply

Your email address will not be published.