Questions tagged [userdefaults]

507 questions
2
votes
1 answer

userdefaults not being saved after first time

I created some simple system that if userdefaults have key "isWalkthroughPresented". If the key is false then show walkthourghViewController. If it doesn't have the key then check from database. However it doesn't set the key after first time. But…
Xistence
  • 23
  • 4
1
vote
1 answer

Change white and dark mode in SwiftUI App and persist it

I want my app to support both light and dark themes, where users can change the theme to their preference or keep it as the default system theme. When users switch the theme to dark or light mode, the app should remember their choice and apply the…
1
vote
1 answer

Why doesn't willSet trigger for an @AppStorage variable when it's directly bound to a SwiftUI Toggle?

I'm using @AppStorage along with a SwiftUI Toggle to manage a UserDefaults variable. My expectation is to perform some operations when the variable changes due to the Toggle interaction, so I need to execute some code in the willSet part of my…
wssssh
  • 13
  • 2
1
vote
0 answers

How to add and remove to favourites view on heart button press in swiftui

I have a struct and I displayed the product list screen from that struct.There is a heart button in product list view, I want to add the particular item to favourite view on button press and also button color needs to change.and also can remove the…
1
vote
0 answers

NSKeyedUnarchiver validateAllowedClass:forKey: allowed unarchiving safe plist type ''NSString' - even though I list it out

I am using this code to unarchive an array of a custom object from UserDefaults: guard let data = UserDefaults.standard.data(forKey: "key") else { return } let customClassArray = try NSKeyedUnarchiver.unarchivedObject(ofClasses: …
Micro
  • 10,303
  • 14
  • 82
  • 120
1
vote
2 answers

How to convert a time format to Int, do some calculations with it and convert it back to a time format?

I am trying to convert the current time to an Int (without the AM/PM addition) and doing calculations with it. For instance, compare it to an older time and calculate the difference, then show the difference in hours:minutes. But I am really…
Hej-j
  • 11
  • 2
1
vote
1 answer

Swiftui - Syncing @AppStorage data with iCloud

I have a test iPhone app where user has scores that keep updating. Currently I use @AppStorage and that works great. Issue is, if I create iPad app the data won't sync. I tried NSUbiquitiousKeyValueStore, but it's too slow. Scores don't update…
Sunny
  • 31
  • 5
1
vote
0 answers

Using UserDefaults come: Publishing changes from within view updates is not allowed, this will cause undefined behavior

I'm working with UserDefaults im my ViewModel: class SettingsViewModel: ObservableObject { private let cancellable: Cancellable private let defaults: UserDefaults let objectWillChange = PassthroughSubject() private enum Keys { …
Jameel
  • 1,126
  • 1
  • 14
  • 27
1
vote
1 answer

How Change @ObservedResults filter with UserDefaults (In SwiftUI)

Here is my Code Block @AppStorage("shareSelection") var shareSelection = "BTCUSDT" @ObservedResults(coinModel.self, where: ( { $0.s == (UserDefaults.standard.object(forKey: "shareSelection") as? String) ?? ""})) var coinsResult var body: some…
1
vote
1 answer

Is UserDefaults a bad practice to cache data?

As a newbie on iOS, I am devoloping a small app. There are 5 different pages and 4 of them has an imageView for user's profile photo, I am storing and downloading profile photo in Firebase. Is it a bad practice to store the profile photo on…
traglen
  • 31
  • 5
1
vote
0 answers

How to listen for change in UserDefaults in SwiftUI App - Special case

I am working on an iOS app which has a watchOS app as well. I have a settings bundle so when some settings is changed in the iPhone's settings application, the userdefaults of the app will be changed. And whenever a userdefault is updated, I need to…
Mano
  • 670
  • 1
  • 9
  • 28
1
vote
1 answer

Swift UserDefaults publisher for non-standard suite not working correctly

Please see the MRE below. Changing UserDefault suite to a custom one doesn't update the subscription beyond the first value. public extension UserDefaults { @objc dynamic var value1: Int { get { integer(forKey: "value1") } set {…
Martin
  • 1,112
  • 1
  • 11
  • 31
1
vote
1 answer

Obtain User Defaults in Different ViewController

I have my user defaults defined as follow in ViewControllerSettings public let kAlertsKey = "ALERTS_KEY" And it is modified through a switch and an ìf statement with the following code if switchAlerts.isOn{ …
Emilio CG
  • 27
  • 5
1
vote
1 answer

Fatal error when publishing UserDefaults with Combine

I try to observe my array of custom objects in my UserDefaults using a Combine publisher. First my extension: extension UserDefaults { var ratedProducts: [Product] { get { guard let data =…
mike22
  • 33
  • 5
1
vote
2 answers

Swift UserDefaults not persisting after relaunching app

I am having a problem with my UserDefault settings not persisting when my app launches. I have read other posts on this matter and most are marked solved after various tips. I have implemented all of the suggested tips (that I am aware of) and I…
Joel S.
  • 41
  • 5