-2

Im trying to pass two kinds of different JSON data in an app with two different views in two navigationlinks, what works

One custom view which with the name LaundryList that gets passed in the MainView, the results are fine here, it does as expected, it shows my JSON data in a list. The problem is here:

I have another NavigationLink just below with another custom view with the name TumbleView as the label and want to pass data in my TumblingList, but its asking me for for an Tumbling array. and gives me the error code Cannot convert value of type Laundry to expected argument type Tumbling array when I pass in the data.

Why can't I use just the same way I accessed the other properties in my navigationLink above?

All my structs follows the right protocols etc

struct Laundry: Identifiable, Codable {

    let id = UUID()
    var image: String
    var description: String
    var summary: String
    var tumbling: [Tumbling]

}

struct Tumbling: Identifiable, Codable {
    
    let id = UUID()
    var image: String
    var description: String
    var summary: String

}

struct WashListRow: View {
    
    var laundry: Laundry
    
    var body: some View {
        
        HStack(spacing: 30) {
            
            Image(laundry.image)
                .resizable()
                .aspectRatio(contentMode: .fit)
                .frame(width: 35, height: 35)
                
            
            Text(laundry.description)
                .font(.callout)

        }
            
        
        
    }
}

#Preview {
 WashListRow(laundry: Laundry(image: "wash60", description: "Normal Wash at 60°", summary: "more dummy data", tumbling: [Tumbling(image: "wash60", description: "test", summary: "more dummy data")]))
}


struct TumbleListRow: View {
    
    var tumble: Tumbling
    
    var body: some View {
        
        HStack(spacing: 30) {
            
            Image(tumble.image)
                .resizable()
                .aspectRatio(contentMode: .fit)
                .frame(width: 35, height: 35)
                
            
            Text(tumble.description)
                .font(.callout)

        }
        
    }
}

#Preview {
    TumbleListRow(tumble: Tumbling(
        image: "lowDrying",
        description: "tumbling",
        summary: "Tumble in low heat"))
}

struct LaundryList: View {
    
    var laundry: [Laundry]
    
    var body: some View {
        

            VStack {
                
                List(laundry) { item in
                    
                    WashListRow(laundry: item)
                    
                    
                }
                

            }

    }
}

#Preview {
    
    LaundryList(laundry: [Laundry(image: "wash30", description: "mock data", summary: "more mock data", tumbling: [Tumbling]())])
    
}



struct TumblingList: View {
    
    var tumbling: [Tumbling]
   
    
    var body: some View {
        
        VStack {
            
            List(tumbling) { item in
                
                TumbleListRow(tumble: item)
                
                
            }
            
        }
        
    }
}

#Preview {
    TumblingList(tumbling: [Tumbling(image: "highDrying", description: "mock data", summary: "more dummy data")])
}

struct MainView: View {
    
    @State var clean = [Laundry]()
    var dataService = FetchData()
    
    
    var body: some View {
        
        NavigationStack {
            
            VStack(spacing: 30) {
                
                
                
                Text("Take care of your wash")
                    .font(.title2)
                    .padding(.top, 40)
                
                Spacer()
                
                
                NavigationLink {
                    LaundryList(laundry: clean)
                } label: {
                    LaundryView()
                }

                
                
                
                NavigationLink {
//                    TumblingList(tumbling: clean)
                } label: {
                    TumbleView()
                }


                Spacer()
                
                
            }.onAppear(perform: {
                clean = dataService.getLocalData()
            })
        }`
    }
}

#Preview {
    MainView()
}
2
  • 1
    Please post the code as text not images. Commented Jan 30, 2024 at 19:25
  • reformatted to code, thanks. Commented Jan 31, 2024 at 18:30

1 Answer 1

0

In your TumblingList, you declared tumbling to be an array of Tumbling. However your clean variable is an array of Laundry. Laundry contains an array of Tumbling but it is not the same object type. Not understanding the specifics of what you're trying to do but if you want to pass in the Tumbling object thats part of your Laundry object, you would probably iterate through your clean array like so:

    var clean = [Laundry(image: "washer", description: "laundry 1", summary: "laundry 1 s", tumbling: [Tumbling(image: "dryer", description: "dryer1", summary: "dryer1s")]), Laundry(image: "washer", description: "laundry 2", summary: "laundry 2 s", tumbling: [Tumbling(image: "dryer", description: "dryer2", summary: "dryer2s")])
]

var body: some View {
    NavigationStack {
        VStack {
            Text("Take care of your wash")
            Spacer()
            NavigationLink {
                LaundryList(laundry: clean)
            } label: {
                Text("Laundry View")
            }
            List(clean) { laundryItem in
                
                NavigationLink {
                    TumblingList(tumbling: laundryItem.tumbling)
                } label: {
                    Text("Tumbling View")
                }
            }
        }
    }
}

ps: please use code snippets instead of images next time

Sign up to request clarification or add additional context in comments.

2 Comments

very useful! but are you suggesting that I remove the state property? Im trying to access the tumbling properties for them to show in a custom view that I have made. Since my Laundry struct has an array of a tumbling property, I tought that's the way to go.
you can keep it as state if you like, my code is just any example of how you would actually get the tumblings into TumblingList. Main problem you're having is that your custom view TumblingList have var tumbling: [Tumbling], however what you are passing in is [Laundry]. Your [Tumbling] is inside a piece of Laundry which is then accesses each piece of Laundry you have in your [Laundry] to access the [Tumbling]. (clean[0].tumbling)

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.