how to set color from dictionary type Api in SwiftUI?

Viewed 31

This is My Model And i don't understand how to use?

import Foundation

public struct PublisherModel: Decodable {
    public let colorlists: [String: String]

    public init(colorlists: [String: String]) {
        self.colorlists = colorlists
    }
}

This is my View Model I know this is not correct but i can not understand how to use of the dictionary in API import SwiftUI

class PublisherModelVM: ObservableObject {
    @Published var datas = [PublisherModel]()
    
    let url = "https://www.alibrary.in/api/web-home"
    
    init() {
        getData(url: url)
    }
    
    
    func getData(url: String) {
        guard let url = URL(string: url) else { return }
        URLSession.shared.dataTask(with: url) { (data, _, _) in
            if let data = data {
                do {
                    let results = try JSONDecoder().decode(PublisherModel.self, from: data)
                    DispatchQueue.main.async {
                        self.datas = results.colorlists
                      
                      
                    }
                }
                catch {
                    print(error)
                }
            }
        }.resume()
    }
}

And I want to set the color in our View By Api value How can I set.

And this is My Api. I want to the colorlists value fetch and set in My view

 struct PublisherList: View {
            
        @StateObject var fetching = PublisherModelVM()
        
        var layout: [GridItem] = [
               GridItem(.flexible(), spacing: nil, alignment: nil),
               GridItem(.flexible(), spacing: nil, alignment: nil)
           ]
        var body: some View {
           
               
                        ScrollView{
                           
                                ForEach(fetching.datatotal, id: \.id) { i in
                                 Vstack{

                                Text("Hello Word")
.background(Color.black)
}
                        
                                 
                                }.frame(width: 180, height: 245)
.background(Color(i.colorlists))//i dont understand
      
    
       }
        }
    }

please help me Im a native user of SwiftUI. Thank you in advance

0 Answers
Related