Questions tagged [nsuserdefaults]

NSUserDefaults is the Objective-C API for storing and retrieving user preferences in Apple's Foundation framework for Cocoa and Cocoa Touch. It is available in OS X v10.0 and later and Available in iOS 2.0 and later which is inherited from NSObject root class

NSUserDefaults provides a cross-process hierarchical key-value store for use in Cocoa and Cocoa Touch applications. It is equivalent to and compatible with the CFPreferences C API in CoreFoundation.

This class provides a programmatic interface for interacting with the defaults system.At runtime, NSUserDefaults object is used to read the defaults that an application uses from a user’s defaults database. NSUserDefaults caches the information to avoid having to open the user’s defaults database each time you need a default value . The questions related to this can be tagged with

Source:

Question related to this are:

  1. iOS: How to store username/password within an app?

  2. Clearing NSUserDefaults

  3. How to store custom objects in NSUserDefaults

  4. Easy way to see saved NSUserDefaults?

3775 questions
59
votes
5 answers

Save dictionary in userdefaults in swift 3 with xcode 8

I am using the following code to save an object to UserDefaults (previously NSUserDefaults) using xcode 8: let defaults = UserDefaults.standard() defaults.set(someObject, forKey: "someObject") print(defaults.object(forKey: "someObject")) someObject…
alionthego
  • 8,508
  • 9
  • 52
  • 125
57
votes
8 answers

Saving UIColor to and loading from NSUserDefaults

What's the easiest way to save a UIColor into NSUserDefaults and then get it back out?
Unreality
  • 4,111
  • 11
  • 48
  • 67
56
votes
11 answers

How can I reset the NSUserDefaults data in the iPhone simulator?

I've added NSUserDefaults data retrieval to my app, which is pretty nice. But for testing I would like to reset all the data I added to the defaults database, so that everything is in the state when the user launches the app the first time. I tried…
Thanks
  • 40,109
  • 71
  • 208
  • 322
56
votes
11 answers

Delete key values for NSUserDefaults in Swift

How to delete data from NSUserDefaults? There is quite a few answers how to do it in Objective C, but how about Swift? So I tried this: let defaults = NSUserDefaults.standardUserDefaults() defaults.removeObjectForKey("myKey") Didn't work. Maybe…
Xernox
  • 1,706
  • 1
  • 23
  • 37
54
votes
10 answers

Swift - How to set initial value for NSUserDefaults

I have a switch called soundSwitch, I'm saving the state of the button using an userDefault as such: @IBAction func soundsChanged(sender: AnyObject) { if soundSwitch.on{ defaults.setBool(true, forKey: "SoundActive") …
Big Green Alligator
  • 1,175
  • 3
  • 17
  • 33
52
votes
4 answers

Why is NSUserDefaults not saving my values?

Hi I am trying to use NSUserDefaults to save some default values in database. I am able to save the values in the NSUserDefaults (even checked it in NSLog). Now I need the values in app delegate when the application is restarted. But I am not…
pankaj
  • 7,878
  • 16
  • 69
  • 115
51
votes
9 answers

Should I use NSUserDefaults or a plist to store data?

I will be storing a few strings (maybe 10-20). I am not sure if I should use NSUserDefaults to save them, or write them out to a plist. What is considered best practice? NSUserDefaults seems like it is less lines of code, therefore quicker to…
Sheehan Alam
  • 60,111
  • 124
  • 355
  • 556
47
votes
4 answers

NSUserDefaults losing its keys & values when phone is rebooted but not unlocked

We are currently experiencing the following weird issue with our iPhone app. As the title says, NSUserDefaults is losing our custom keys and values when phone is rebooted but not unlocked, and this is happening on a very specific scenario. Context:…
mp3821
  • 841
  • 1
  • 10
  • 18
47
votes
5 answers

How to set NSConstraintBasedLayoutVisualizeMutuallyExclusiveConstraints?

I am getting the following message from Cocoa Auto Layout mechanism: Set the NSUserDefault NSConstraintBasedLayoutVisualizeMutuallyExclusiveConstraints to YES to have -[NSWindow visualizeConstraints:] automatically called when this happens. But I…
Yoav
  • 5,962
  • 5
  • 39
  • 61
46
votes
4 answers

What are the limitations of NSUserDefaults?

Storing data permanently in an iPhone is usually done using Core Data or sqlite3. Most people prefer to user NSUserDefaults as a storage for application preferences, rather than using it as a normal database (such as sqlite). I have found that large…
SEG
  • 1,717
  • 1
  • 18
  • 33
43
votes
4 answers

How to set initial values for NSUserDefault Keys?

I want to set some initial values for my NSUserDefault keys so that the first run of the app has some reasonable initial settings. I thought I ran across a simple way to do this in the app bundle .plist, but now I can't find it. Any ideas?
Suz
  • 3,744
  • 3
  • 23
  • 26
40
votes
5 answers

Store [String] in NSUserDefaults

I want to save a Swift Style String Array into NSUserDefaults, but acutally the "if" statement in the code says that returnValue is always nil. Later in the code (iOS 8) I want to use "food += ["spaghetti"] to add new entries. var food : [String] { …
Thomas
  • 1,468
  • 4
  • 14
  • 20
39
votes
1 answer

argument for generic parameter could not be inferred

I'm trying to save an array with NSUserDefaults and then load the array, but I get the error "argument for generic parameter could not be inferred." Is there anything I am doing wrong? No one seems to be having this problem in swift, so I can't find…
l-spark
  • 871
  • 2
  • 10
  • 25
38
votes
7 answers

How do I use UserDefaults with SwiftUI?

struct ContentView: View { @State var settingsConfiguration: Settings struct Settings { var passwordLength: Double = 20 var moreSpecialCharacters: Bool = false var specialCharacters: Bool = false var…
SmushyTaco
  • 1,421
  • 2
  • 16
  • 32
37
votes
2 answers

Best Practice for NSUserDefaults synchronize

I'm using [[NSUserDefaults standardUserDefaults] synchronize] each time I write anything to the plist. Is that overkill? Or are there adverse effects in doing this?
Lauren Quantrell
  • 2,647
  • 6
  • 36
  • 49