I have the following code:
struct Car: Hashable, Identifiable, Codable {
var id = UUID()
var year:Int
var make:String
var model:String
var notes:String?
static var testCars:[Car] {
return [
Car(year: Int.random(in: 1990...2023), make: "Honda", model: "S800"),
Car(year: Int.random(in: 1990...2023), make: "Honda", model: "Civic"),
]
}
}
enum CarViewRouter:Hashable, Identifiable, Codable {
static func == (lhs: CarViewRouter, rhs: CarViewRouter) -> Bool {
var lhsCarId:UUID
switch lhs {
case .car(let car):
lhsCarId = car.id
}
var rhsCarId:UUID
switch rhs {
case .car(let car):
rhsCarId = car.id
}
return lhsCarId == rhsCarId
}
var id: UUID {
switch self {
case .car(let car):
return car.id
}
}
case car(Binding<Car>)
}
struct ContentView: View {
@State var cars = [Car]()
@State var path = NavigationPath()
var body: some View {
NavigationStack {
VStack {
ForEach($cars) { $car in
Button {
path.append(CarViewRouter.car($car))
} label: {
Text(car.model)
}
}
}
.task {
cars = Car.testCars
}
.navigationDestination(for: CarViewRouter.self) { d in
switch d {
case .car(let car):
CarDetails(car: car)
}
}
}
}
}
struct CarDetails:View {
@Binding var car:Car
var body: some View {
Text("\(car.make) - \(car.model) - \(car.year)")
}
}
I am unable to make it compile. Errors:
Type 'CarViewRouter' does not conform to protocol 'Decodable'
Type 'CarViewRouter' does not conform to protocol 'Encodable'
Type 'CarViewRouter' does not conform to protocol 'Hashable'
How can I make the CarViewRouter
as defined conform to those protocols so that it compiles?
[added the entire code to provide complete context. I was trying to pass in a Car
so that a subview could eventually edit it...]