I have a struct for ShopsModel that complies to Identifiable.
struct ShopsModel: Identifiable {
var id: Int
var shopName: String
var postcode: String
var type: String
}
I have two shops (there will be more!):
let shop1 = ShopsModel(
id: 1,
shopName: "shop1",
postcode: "PE31 8AA",
type: "Clothing")
let shop2 = ShopsModel(
id: 2,
shopName: "shop2",
postcode: "PE31 8AB",
type: "Clothing")
And my list code
import SwiftUI
struct ShopsList2: View {
let shops2: ShopsModel
var body: some View {
NavigationStack {
List(shops2, id: \.self.id) { shop in
NavigationLink {
ShopsDetail(name: shop)
} label: {
Text(shop)
}
}.navigationTitle("Shops")
}
}
then the error: Initializer 'init(_:id:rowContent:)' requires that 'ShopsModel' conform to 'RandomAccessCollection'
What am I doing wrong?
I added an id to each instance of ShopsModel and I made the struct identifiable. I thought that would get rid of the error. It didn't I also have an error on preview and I can't work out what to put in the parentheses as it doesn't seem to recognise shops2.