1

Hey in my app I am trying to store whether the User wants a location to be its favourite. In order to make the change in this UserDefaults update the view I need to use @AppStorage. However, at the moment I only get the ERROR "No exact matches in call to initializer" Do i need to use Data instead of the [String: String], if yes how? Does anyone know how to fix this? Thanks in advance

import SwiftUI

struct SpotDetailView: View {
    @State var spot: WindApp //knows which spot it's at
    var spotname:String
    //@State var favourites: [String:String] = UserDefaults.standard.object(forKey: "heart") as? [String:String] ?? [:]
    @AppStorage("heart") var favourites: [String: String] = [:]

        
    var body: some View {
        
        ZStack {
            
            List {
                SpotMapView(coordinate: spot.locationCoordinate)
                    .cornerRadius(8)
                ForEach(0..<9) { i in
                    SingleDayView(spot: spot, counter: (i * 8))
                    }
            }
            .navigationTitle("\(spot.spot)")
            VStack {
                Spacer()
                HStack {
                    Spacer()
                    Button(action: {
                        if favourites[spot.spot] == "heart" {
                            favourites[spot.spot] = "heart.fill"
                            UserDefaults.standard.set(favourites, forKey: "heart")
                        }
                        else if favourites[spot.spot] == "heart.fill" {
                            favourites[spot.spot] = "heart"
                            UserDefaults.standard.set(favourites, forKey: "heart")
                        }
                        else {
                            favourites[spot.spot] = "heart.fill"
                            UserDefaults.standard.set(favourites, forKey: "heart")
                        }
                
                    }, label: {
                        Image(systemName: favourites[spot.spot] ?? "heart")
                    })
                    .frame(width: 20, height: 20)
                }
            }
        }
    }
}
  • Does this answer your question? [Using @AppStorage for string map](https://stackoverflow.com/questions/62602279/using-appstorage-for-string-map) – Andrew May 20 '21 at 10:51

1 Answers1

2

You will have to use Data. @AppStorage currently only supports:

  • Int
  • Double
  • String
  • Bool
  • URL
  • Data

See this answer for sample implementation and further details

Andrew Stoddart
  • 250
  • 2
  • 8