Hi, I'm new to SwiftUI and have been trying to accomplish this without success.
I have the following arrays and need to filter out from the options array the items that exist in the selected array.
import SwiftUI
struct Option: Decodable, Hashable {
var userCode: String
var optType: String
var optValue: String
}
struct Selected: Decodable, Hashable {
var userCode: String
var tradeCode: String
var optType: String
var optValue: String
}
struct ContentView: View {
@State var options = [
Option(userCode: "1", optType: "Emotions", optValue: "Sad"),
Option(userCode: "1", optType: "Emotions", optValue: "Happy"),
Option(userCode: "1", optType: "Emotions", optValue: "Angry"),
Option(userCode: "1", optType: "Emotions", optValue: "Calm")
]
@State var selected = [
Selected(userCode: "1", tradeCode: "1", optType: "Emotions", optValue: "Sad"),
Selected(userCode: "1", tradeCode: "1", optType: "Emotions", optValue: "Happy")
]
var body: some View {
Button("Options Not Selected") {
//Here I need an array of the options NOT selected
}
}
}
Any help would be greatly appreciated.
Thanks
It is not clear enough, but you can prepare some extension like this:
extension Selected {
func matches(_ option: Option) -> Bool {
//↓Modify the following expression to fit for your purpose
return optType == option.optType
&& optValue == option.optValue
}
}
And then:
Button("Options Not Selected") {
let optionsNotSelected = options.filter { option in
!selected.contains {$0.matches(option)}
}
print(optionsNotSelected)
//Use `optionsNotSelected`...
}