Questions tagged [userdefaults]
507 questions
3
votes
2 answers
UserDefault is not working with M1 Chip MacBook Air and Xcode Simulator in Swift 5
I have M1 chip MacBook Air laptop. I set, remove and get value from UserDefaults.
Below code I set the value
UserDefaults.standard.setValue("my string", forKey: "UserEmail")
Get the value using valueforkey
UserDefaults.standard.value(forKey:…

Yogesh Patel
- 1,893
- 1
- 20
- 55
3
votes
1 answer
Why is using UserDefaults to store sensitive information on iOS not safe?
From what I understand after reading the iOS Swift documentation, these are the key points:
The user can't access or modify my iOS application's UserDefaults
Other applications can't access or modify my iOS application's UserDefaults
At most,…

AlanSTACK
- 5,525
- 3
- 40
- 99
3
votes
1 answer
Why is UserDefaults.init(suiteName:) a failable initializer?
The UserDefaults.init(suiteName:) initializer is a failable initializer. However, the documentation does not specify the circumstances under which it will return a nil UserDefaults object.
Firstly, what are the circumstances under which it will…

Adil Hussain
- 30,049
- 21
- 112
- 147
3
votes
3 answers
how to save UIImage to UserDefaults in swift 5
I'm trying to save users image to userDefaults but i'm always getting error "Thread 1: "Attempt to insert non-property list object (Function) for key savedimage" Here's my code
import UIKit
class productSelectionPage: UIViewController {
…

Мухаммед Аралбек
- 115
- 3
- 10
3
votes
1 answer
UserDefaults instance for unit testing?
I have a class which I want to test. It uses UserDefaults in its logic.
I know I have to inject a test double to test it, but I want to make it without too much effort.
If it's possible to make a temporary UserDefaults instance which is only valid…

Takeshi Yokemura
- 103
- 9
3
votes
3 answers
Add variables to an already saved User Defaults variable
I have downloaded a swift file that should help me with saving and loading custom variables:
import Foundation
protocol ObjectSavable {
func setToObject

אורי orihpt
- 2,358
- 2
- 16
- 41
3
votes
1 answer
Value of protocol type 'Any' cannot conform to 'Equatable' Swift
I'm trying to remove "songDict" from "libraryArray" but it triggers an error.
var libraryArray = UserDefaults.standard.value(forKey: "LibraryArray") as! [Dictionary]
var songDict = Dictionary()
var arr =…

Sam
- 105
- 1
- 9
3
votes
1 answer
UserDefaults(suiteName:) not propagating between app and extension
While both my app and extension are able to see and read/write to my shared UserDefaults object, accessed in both cases via:
if let userDefaults = UserDefaults(suiteName: "group.markl.wrshared.container") {
...
}
...they can only read data…

Mark
- 51
- 2
3
votes
2 answers
How do I save the current user location in defaults?
I am trying to give users the option to save their current location. I use the defaults to save it and I found out, how to get the users location. But I am not sure how to combine those two tasks. So here is my question:
How can I get my function to…

Roman H
- 251
- 2
- 10
3
votes
2 answers
Save/Get UIColor from UserDefaults
I need some help to load and read UIColor from UserDefaults.
I found a nice extension to do that:
extension UserDefaults {
func colorForKey(key: String) -> UIColor? {
var color: UIColor?
if let colorData = data(forKey: key) {
color =…

Sqweelow
- 187
- 2
- 13
3
votes
1 answer
How to access the UserDefaults file folder on my ios application
I am would like to know how to access the Documents directory of my ios appplication. I have tried the following on xcode:
window > Devices and Simulators > [Select my device] > [select my application by name] > Show container.
The container…

dean
- 321
- 3
- 17
3
votes
2 answers
UserDefaults.didChangeNotification not firing
The project I am working on has an extension that writes data to UserDefaults. Then in the containing app should the UI should get updated according to the changes. The problem is that UserDefaults.didChangeNotification does not get fired unless the…

surToTheW
- 772
- 2
- 10
- 34
3
votes
1 answer
Cannot retrieve Date object from UserDefaults Swift
I'd like to post and retrieve a Date object to UserDefaults.standard. As of now, I post the date object using UserDefaults.standard.set(firstDate, forKey: "Date") and retrieve it with inDate = UserDefaults.standard.object(forKey: "Date") where…

jacob_g
- 674
- 8
- 21
3
votes
1 answer
Save Dictionary of custom objects in Swift using UserDefaults
I'm writing an app to work with contacts and trying to save information with UserDefaults but I'm getting SIGABRT. Here's my code, what I'm doing wrong?
class Contact{
var name = String()
var phone = String()
init(name: String, phone: String){
…

Candis Mayer
- 33
- 2
3
votes
3 answers
UserDefaults between Objective-C and Swift
Just trying to be extra careful here...
If I have an app that saved a value in UserDefaults like this in Objective-C:
NSString *newString = textField.text;
[[NSUserDefaults standardUserDefaults] setObject: newString forKey:@"textKey"];
Would this…

RanLearns
- 4,086
- 5
- 44
- 81