-1

this is my Model

public struct Welcome: Decodable{
    public let userslist: [Userslist]

}


public struct Userslist: Decodable, Hashable{
 public let full_name: String
    public let partner_media: [PartnerMedia]

    public init( partner_media: [PartnerMedia]) {
     

        self.partner_media = partner_media
    }
}

public struct PartnerMedia: Decodable , Hashable{
    public var id = UUID()
    public let url: String

    public init( url: String) {

        self.url = url
    }
}

This is View Model I follow the MVVM pattern for accessing the data from API.

class PublisherModelVM: ObservableObject {
    @Published var datas = [PartnerMedia]()
    
    let url = "APIUrl"
    
    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(Welcome.self, from: data)
                    DispatchQueue.main.async {

                        self.datas = results.userslist `//Cannot assign value of type '[Userslist]' to type '[PartnerMedia]' what should I write for getting proper response`
                        

                    }
                }
                catch {
                    print(error)
                }
            }
        }.resume()
    }
}

I want to fetch the url and full_name And set to the View

 struct PublisherListView: View{
        @StateObject var list = PublisherModelVM()
        
        var body: some View{
            ScrollView(.horizontal,showsIndicators: false){
    
                    ForEach(list.datas, id: \.id){ item in
Text(item.full_name)
                        AsyncImage(url: URL(string: item.url)){image in
                            image
                                .resizable()
                                .frame(width: 235, height: 125).cornerRadius(8)
                            
                        }placeholder: {
                         
                                Image(systemName: "eye") .resizable()
                                    .frame(width: 235, height: 125).cornerRadius(8)
                           
                        }
                        
                    }
                    
                   
                }
                
                
            }
        }

this Error show in my Xcode Cannot assign value of type '[Userslist]' to type '[PartnerMedia]' Please help me. can anyone help me for recommending for API related full detailed courses and thank you in advance

4
  • 1
    According to your model, results.url is an array of Userslist, and datas is an array of PartnerMedia, so you see they aren't the same, right? If you have 2 userlists, and each one have 3 partnerMedia, you want to have one array of 6 partnerMedias? If yes, you can use a flatMap(), to do so, or a manual for loop to retrieve only the partnerMedias from your response. Commented Sep 21, 2022 at 7:33
  • 1
    That code can not generate that compilation error. Post the actual code that gives you a problem. Commented Sep 21, 2022 at 7:36
  • 2
    self.datas = results.userslist Cannot assign value of type '[Userslist]' to type '[PartnerMedia]' @JoakimDanielson and @ Larme sorry for that mistake. what should i write in the coding section please help me. Commented Sep 21, 2022 at 7:45
  • 1
    Please fix your question instead of posting the correct code here in the comments. We can't tell you how to fix it unless you tell us what you want to do, please read the comment by @Larme again and respond to it. Commented Sep 21, 2022 at 7:47

1 Answer 1

1

As I said before (in the questions you have deleted) pay attention to the details of your models to match the json data. Try this approach, works very well for me:

struct ContentView: View {
    var body: some View {
        PublisherListView()
    }
}

struct ServerResponse: Decodable {
    let userslist: [User]
}

struct User: Decodable, Identifiable {
    let id: Int
    let totalBooks: Int
    let totalfollowers: Int
    let fullAddress: String?
    let partnerMedia: [PartnerMedia]
    
    enum CodingKeys: String, CodingKey {
        case id, totalBooks,totalfollowers
        case partnerMedia = "partner_media"
        case fullAddress = "full_address"
    }
}

struct PartnerMedia: Decodable, Identifiable {
    let id: Int
    let url: String
}

struct PublisherListView: View{
    @StateObject var list = PublisherModelVM()
    
    var body: some View{
        ScrollView(.horizontal,showsIndicators: false){
            HStack(spacing:15) {
                ForEach(list.datas, id: \.id){ item in
                    AsyncImage(url: URL(string: item.url)){ image in
                        image
                            .resizable()
                            .frame(width: 235, height: 125).cornerRadius(8)
                        
                    } placeholder: {
                        Image(systemName: "eye") .resizable()
                            .frame(width: 235, height: 125).cornerRadius(8)
                    }
                }
            }
        }
    }
}

class PublisherModelVM: ObservableObject {
    @Published var datas = [PartnerMedia]()
    
    let url = "https://alibrary.in/api/publisherList"
    
    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(ServerResponse.self, from: data)
                    DispatchQueue.main.async {
                        for user in results.userslist {
                            self.datas.append(contentsOf: user.partnerMedia) 
                        }
                    }
                }
                catch {
                    print(error)
                }
            }
        }.resume()
    }
    
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.