userdefaults

Sharing UserDefaults between extensions

强颜欢笑 提交于 2019-11-29 05:35:44
问题 Creating a Today widget and I am using UserDefaults(suiteName:) to persist some data. In the main application I am using UserDefaults.standard() . This can't be read (or can it?) by the extension which is why I use the suiteName: constructor. Data that user persist to UserDefaults.standard() in the main app needs to be available in the extension. At this time I am persisting to both so that the values can be shared UserDefaults.standard().set:...forKey:... UserDefaults(suiteName:...)().set:..

How can I use UserDefaults in Swift?

时光毁灭记忆、已成空白 提交于 2019-11-26 14:52:52
How can I use UserDefaults to save/retrieve strings, booleans and other data in Swift? Anbu.Karthik ref: NSUserdefault objectTypes Swift 3 and above Store UserDefaults.standard.set(true, forKey: "Key") //Bool UserDefaults.standard.set(1, forKey: "Key") //Integer UserDefaults.standard.set("TEST", forKey: "Key") //setObject Retrieve UserDefaults.standard.bool(forKey: "Key") UserDefaults.standard.integer(forKey: "Key") UserDefaults.standard.string(forKey: "Key") Remove UserDefaults.standard.removeObject(forKey: "Key") Remove all Keys if let appDomain = Bundle.main.bundleIdentifier { UserDefaults