How do I make this work?
Rather than duplicating the MenuItem struct, I would like to be able to pass in any object conforming to Storage
If I change 'var items: [Storage]' to just [Wallet] or [Bank] it works fine. But when it is [Storage], ForEach complains
I'm new to Swift and trying hard to understand Hashable to resolve this issue but I need help.
protocol Storage {
var index: Int { get }
var name: String { get }
// ...
}
struct Bank: Storage, Hashable {
let index: Int
let name: String
// ...
func hash(into hasher: inout Hasher) {
hasher.combine(index)
}
}
struct Wallet: Storage, Hashable {
let index: Int
let name: String
// ...
func hash(into hasher: inout Hasher) {
hasher.combine(index)
}
}
struct MenuItem: View {
// I need this to except objects conforming to the Storage protocol
var items: [Storage]
var body: some View {
VStack(spacing: 0) {
// ... Type 'any Storage' cannot conform to 'Hashable'
ForEach(items, id: \.self) {item in
// ... This thows the error
}
}
}
}
var banks = [Bank]()
var wallets = [Wallet]()