Questions tagged [userdefaults]
507 questions
160
votes
16 answers
How can I use UserDefaults in Swift?
How can I use UserDefaults to save/retrieve strings, booleans and other data in Swift?

Mithun Ravindran
- 2,292
- 2
- 14
- 23
81
votes
6 answers
Sharing UserDefaults between extensions
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…

RyanTCB
- 7,400
- 5
- 42
- 62
65
votes
8 answers
Sharing data between an iOS 8 share extension and main app
Recently, I've been making a simple iOS 8 share extension to understand how the system works. As Apple states in its App Extension Programming Guide:
By default, your containing app and its extensions have no direct access to each other’s…

Oguz Bilgener
- 755
- 1
- 10
- 17
38
votes
7 answers
SwiftUI: What is @AppStorage property wrapper
I used to save important App data like login credentials into UserDefaults using the following statement:
UserDefaults.standard.set("sample@email.com", forKey: "emailAddress")
Now, I have come to know SwiftUI has introduced new property wrapper…

Shawkath Srijon
- 739
- 1
- 8
- 17
21
votes
13 answers
scrollToItem isn't working in my CollectionView?
Why scrollToItem isn't working in my CollectionView? I want to work scrollToItem on EnabledID. So what's a problem and how fixed it?
Code:
var dataArray = NSMutableArray() // dataArray is has a data from Database
var EnabledID = Int()
EnabledID =…

developer1996
- 827
- 2
- 14
- 26
9
votes
2 answers
How to save array of tuples in UserDefaults
I am using a tuple of arrays in one of my files and want to start saving it in UserDefaults. Here is how I am storing my current data:
typealias savemytuple = (Name: String, NameID: String, BookID: String, Picture: NSData)
var savefun =…

mbro12
- 101
- 2
- 3
9
votes
2 answers
How to save an Array with Objects to UserDefaults
My Object conforms to the new Swift 4 Codeable protocol. How to save an array of these Objects in UserDefaults?
struct MyObject: Codeable {
var name: String
var something: [String]
}
myObjectsArray = [MyObject]() // filled with…

Jan
- 12,992
- 9
- 53
- 89
9
votes
1 answer
Cannot set non-property-list object in UserDefaults
I have a simple function in which I save an object to user defaults for the purpose of loading it back in later, it looks like this:
func saveGame() {
// hero is an instance of the class Hero, subclassed from SKSpriteNode
…

zeeple
- 5,509
- 12
- 43
- 71
8
votes
2 answers
Removing UserDefaults-generated plist file when running unit tests
I'm testing a class that depends on an instance of UserDefaults. In following this sample code found here, I create and setup the instance like so:
override func setUp() {
super.setUp()
defaults = UserDefaults(suiteName: #file)
…

WongWray
- 2,414
- 1
- 20
- 25
8
votes
1 answer
Updating a saved Codable struct to add a new property
My app uses a codable struct like so:
public struct UserProfile: Codable {
var name: String = ""
var completedGame: Bool = false
var levelScores: [LevelScore] = []
}
A JSONEncoder() has been used to save an encoded array of…

RanLearns
- 4,086
- 5
- 44
- 81
8
votes
4 answers
Saving a Codable Struct to UserDefaults with Swift
I am trying to encode a struct
struct Configuration : Encodable, Decodable {
private enum CodingKeys : String, CodingKey {
case title = "title"
case contents = "contents"
}
var title : String?
var contents:…

F. Pizzuta
- 107
- 1
- 3
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
6
votes
2 answers
How do you save Color Picker values into UserDefaults in SwiftUI?
I have an array of colors assigned that I want saved to UserDefaults. I have a default of gray, green, orange, and red, but I want to allow a color picker to change the green orange and red. The default value works and shows in my Simulator, but…

Nate Rose
- 61
- 1
- 3
6
votes
2 answers
How to write Unit Test for UserDefaults
I have 2 functions where one basically retrieves string in User Defaults and another writes the string in User Defaults. I'm not able to understand that should I do the unit test or no? and I'm pretty new to the concept for Unit testing.
I scoured…

Kane D
- 63
- 1
- 4
6
votes
2 answers
"Value for key 'root' was unexpected class 'NSArray'" error when retrieve an array of object in UserDefault, Swift
I'm trying to retrieve an array of custom object in Swift, where i get the error
"UserInfo={NSDebugDescription=value for key 'root' was of unexpected class 'NSArray'. Allowed classes are '{(MusicCloud.Playlist)}"
My project is a music player, so…

hrtlkr29
- 383
- 1
- 7
- 21