0

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.

sadel
  • 31
  • 7
  • 1
    Does this answer your question? [How to Store Nested Arrays in @AppStorage for SwiftUI](https://stackoverflow.com/questions/63166706/how-to-store-nested-arrays-in-appstorage-for-swiftui) – jnpdx Nov 09 '21 at 17:46
  • 1
    Depending upon the amount of data you are storing, you can get a performance hit from `@AppStorage`. If you have a lot of data, you will want to store it in a proper database like CoreData. – Yrb Nov 09 '21 at 20:13

0 Answers0