nsuserdefaults
Values not always persisted in App group between companion app & app extension
You must request open access in order to access shared NSUserDefaults. It’s stated directly in the App Extension Programming guide: By default, a keyboard has no network access and cannot share a container with its containing app. To enable these things, set the value of the RequestsOpenAccess Boolean key in the Info.plist file to YES. … Read more
iPhone – How to detect if a key exists in NSUserDefaults standardUserDefaults
Do it the right way and register default values. NSDictionary *userDefaultsDefaults = @{ @”SomeKey”: @NO, @”AnotherKey”: @”FooBar”, @”NumberKey”: @0, }; [NSUserDefaults.standardUserDefaults registerDefaults:userDefaultsDefaults]; do this before you use anything from NSUserDefaults. The beginning of application:didFinishLaunchingWithOptions: is a safe place. You have to register the defaults each time the app launches. NSUserDefaults only stores values that have … Read more
How to register user defaults using NSUserDefaults without overwriting existing values?
From the documentation for -registerDefaults: (emphasis added): The contents of the registration domain are not written to disk; you need to call this method each time your application starts. You can place a plist file in the application’s Resources directory and call registerDefaults: with the contents that you read in from that file. So your … Read more
How to save an array of objects to NSUserDefault with swift?
Swift 4 We need to serialize our swift object to save it into userDefaults. In swift 4 we can use Codable protocol, which makes our life easy on serialization and JSON parsing Workflow(Save swift object in UserDefaults): Confirm Codable protocol to model class(class Place : Codable). Create object of class. Serialize that class using JsonEncoder … Read more
Best Practice for NSUserDefaults synchronize
Yes it may be overkill but in a simple application will you notice a performance hit? probably not if you are only saving after basic user interaction such as the user selecting their settings. The benefit to calling synchronize more often is if your application may crash and the information you are saving is important, … Read more
How do I use UserDefaults with SwiftUI?
The approach from caram is in general ok but there are so many problems with the code that SmushyTaco did not get it work. Below you will find an “Out of the Box” working solution. 1. UserDefaults propertyWrapper import Foundation import Combine @propertyWrapper struct UserDefault<T> { let key: String let defaultValue: T init(_ key: String, … Read more