Questions tagged [appstorage]

56 questions
7
votes
0 answers

Storing arrays of string persistently in SwiftUI using AppStorage

I'm trying to store an array of string in user defaults using the AppStorage property wrapper like this: @AppStorage("History") var history: [String] = ["End of history"] however I get the error No exact matches in call to initializer Is this a…
Arnav Motwani
  • 707
  • 7
  • 26
7
votes
2 answers

How to store EnumTypes with @AppStorage in SwiftUI

@pretep Hi, I want to store a map-status in UserDefaults. Is it possible to do something like this: @AppStorage("myMapType") var mapType: MKMapType = .standard Or do I have to access the rawValue of the MKMapType instead? How could I do this?…
Peter Plass
  • 81
  • 1
  • 5
7
votes
1 answer

Can @AppStorage be used in the Environment in SwiftUI?

Can @AppStorage be used in the Environment in SwiftUI, if so, how would you do it? I know you can send the value for the @AppStorage from one view to another using @Bindings as a general wondering I would like to know if its possible to put it in…
Mark
  • 16,906
  • 20
  • 84
  • 117
5
votes
1 answer

@App Storage and MKCoordinateRegion Function

Is it possible to save an MKCoordinateRegion Value in @App Storage? I tried this. But it doesn't work. I got the error 'No exact matches in call to initializer' . @AppStorage("region_key") var region = MKCoordinateRegion( center:…
comhendrik
  • 239
  • 2
  • 11
5
votes
1 answer

How to use @AppStorage inside a model with @Published and store In App Purchased products

I know that @AppStorage has to be used only inside a view, but I prefer to manage the model and use the advantage of @AppStorage. For example, if I would like to store the purchased product in In App Purchasing, could I use this work around or it is…
Simone Pistecchia
  • 2,746
  • 3
  • 18
  • 30
5
votes
2 answers

How can I store a variable as the key of an @AppStorage variable

So I wanted to make the switch to @AppStorage in my app but I'm troubled with a specific issue. For the key of the @AppStorage I would like to use a variable that I already have but I can not do that but I used to find a way to do it with User…
peterk
  • 319
  • 3
  • 10
4
votes
2 answers

SwiftUI - AppStorage doesn't work with GeometryReader

Here is a simple example. You can create new SwiftUI iOS project and copy it to ContentView file. import SwiftUI struct Settings { static let onOff = "onOff" } struct ContentView: View { @AppStorage(wrappedValue: false, Settings.onOff) var…
mallow
  • 2,368
  • 2
  • 22
  • 63
3
votes
2 answers

Initialize optional @AppStorage property with non-nil value

I need an optional @AppStorage String property (for a NavigationLink selection, which required optional), so I declared @AppStorage("navItemSelected") var navItemSelected: String? I need it to start with a default value that's non-nil, so I…
Steve M
  • 9,296
  • 11
  • 49
  • 98
3
votes
1 answer

SwiftUI - @AppStorage variable key value

I'm trying to get from UserDefaults a Bool whose key depends on the nameID of a Product. I tried this way: import SwiftUI struct ProductDetail: View { var product: GumroadProduct …
Albifer
  • 167
  • 2
  • 10
2
votes
1 answer

App crashes when setting custom Struct value in AppStorage

I have a custom struct I want to store in AppStorage: struct Budget: Codable, RawRepresentable { enum CodingKeys: String, CodingKey { case total, spent } var total: Double var spent: Double init(total: Double = 5000.0,…
Kex
  • 8,023
  • 9
  • 56
  • 129
2
votes
0 answers

@State with a @Appstorage property does not update a SwiftUI View

I organized some settings to be stored in UserDefauls in a struct like this, because I want to have them in one place and to have getters and Setters. enum PrefKeys : String { case KEY1 case KEY2 var key: String { return…
mica
  • 3,898
  • 4
  • 34
  • 62
2
votes
1 answer

@AppStorage not updating view

I have the following reproduced example in Swift/SwiftUI. Intended Functionality: List of numbers, where upon deletion of any number, the numbers update and reorganize so that they are in consecutive order. Example: Suppose we have numbers 1, 2, 3,…
Stoic
  • 945
  • 12
  • 22
2
votes
1 answer

SwiftUI @AppStorage doesn't refresh in a function

I am trying to reload the data every .onAppear, but if I change the @AppStorage nearMeter's value in the SettingsView, it isn't updating the value in the reloadNearStops func and using the previous @AppStorage value. struct SettingsView: View { …
sandorb
  • 479
  • 1
  • 7
  • 17
2
votes
2 answers

Using @AppStoarge with a custom object array does not persist data

I am fairly new to SwiftUI and want to use the @AppStorage property wrapper to persist a list of custom class objects in the form of an array. I found a couple of posts here that helped me out creating the following generic extension which I have…
svcho
  • 23
  • 3
1
vote
1 answer

@AppStorage in Structs

I am really stuck on this. I want to make several button, each having independet state of ifButtonDisabled. The button disables when user clicks on it. Since, I want my app to remember the state of the button when the app reloads or refresh, I am…
Uday Agarwal
  • 7
  • 1
  • 3
1
2 3 4