I create the empty savedList in my ViewModel
@Published var savedList:[Categories] = [Categories]()
Then I create the code so that any restaurant can be saved and then it is added to the array
struct DetailView2: View {
var restaurantInfo:Categories
@EnvironmentObject var a:CategoriesModel
@State var isSaved = false
var body: some View {
VStack {
ZStack(alignment: .bottomLeading) {
Image(restaurantInfo.pic)
.resizable()
.scaledToFit()
Button(action: {
if isSaved == true {
let i = a.savedList.firstIndex(of: restaurantInfo)
a.savedList.remove(at: i!)
isSaved = false
} else if isSaved == false {
a.savedList.append(restaurantInfo)
isSaved = true
}
}, label: {
if a.savedList.contains(restaurantInfo) {
Image(systemName: "bookmark.fill")
.padding()
.background(Color.blue)
.foregroundColor(.white)
.cornerRadius(12)
.padding()
} else {
Image(systemName: "bookmark")
.padding()
.background(Color.blue)
.foregroundColor(.white)
.cornerRadius(12)
.padding()
}
})
}
This code saves the restaurants properly, but how do I make it so that the array remains saved even after any particular user exits the app and re enters it. @AppStorage doesn't work for arrays.