Hi there,
I am receiving a Instance member 'features' cannot be used on type 'DataModel'; did you mean to use a value of this type instead?
error on the PageView(pages: DataModel.features.map { FeatureCard(product: $0) })
line.
features
is apparently defined in my DataModel()
file: var features: [Product] { products.filter { $0.isFeatured } }
I am modifying the Apple SwiftUI Tutorial code.
this is my view:
struct SearchView: View {
@StateObject var model = DataModel()
@State private var searchText = ""
@State private var showSheet = false
var body: some View {
NavigationStack {
List {
ForEach(filteredProducts, id: \.self) { product in
NavigationLink {
Text(product.name)
} label: {
PageView(pages: DataModel.features.map { FeatureCard(product: $0) })
.aspectRatio(3 / 2, contentMode: .fit)
.listRowInsets(EdgeInsets())
.onTapGesture {showSheet = true}
}
}
}
.navigationTitle("Search")
.navigationBarTitleDisplayMode(.inline)
}
.searchable(text: $searchText, prompt: "Tap to Search")
.sheet(isPresented: $showSheet) {
ProductDetail(product: <#Product#>)
}
}
var filteredProducts: [Product] {
if searchText.isEmpty {
return model.products
} else {
return model.products.filter {
$0.name.lowercased().contains(searchText.lowercased()) ||
$0.flavour.lowercased().contains(searchText.lowercased())
}
}
}
}
struct SheetView: View {
let product: Product
var body: some View {
VStack {
Text(product.name)
}
}
}
and if you need it this is my Product()
file:
import Foundation
import SwiftUI
struct Product: Hashable, Codable, Identifiable {
var id: Int
var name: String
var flavour: String
var description: String
var isFeatured: Bool
var category: Category
enum Category: String, CaseIterable, Codable {
case drink = "Drink"
case yoghurt = "Yoghurt"
}
var image: String
private var imageName: Image {
Image(image)
}
var image2: String
private var image2Name: Image {
Image(image2)
}
var image3: String
private var image3Name: Image {
Image(image3)
}
var featureImage: Image? {
isFeatured ? Image(image) : nil
}
}