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()
}
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:
ps: please use code snippets instead of images next time