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
16
votes
4 answers

iPhone: Where NSUserDefaults get stored?

I am able to store, retrieve, and remove values using NSUserDefaults. I couldn't succeed to find out where NSUserDefaults getting stored physically on iPhone? I guess it should be plist file but not sure. Does anyone know if its possible to see this…
Paresh Masani
  • 7,474
  • 12
  • 73
  • 139
16
votes
2 answers

Best way to save to nsuserdefaults for custom class?

If I have a custom class Person which has three variables (which are propertized and synthesized): NSString* theName; float* theHeight; int theAge; Person instances are stored in an NSArray 'Group'. There is only one Group. What is the best way of…
cannyboy
  • 24,180
  • 40
  • 146
  • 252
16
votes
1 answer

NSUserDefaultsDidChangeNotification and Today Extensions

I am developing an iPhone app with a Today Extension. The app has a Model module that loads from/saves toNSUserDefaults. Since I want this information to be available to both the main app and the extension, I use an app group: let storage =…
Alex
  • 5,009
  • 3
  • 39
  • 73
16
votes
2 answers

OS X: NSUserDefaults not saved

I have a sandboxed app for OSX and I saved some data in NSUserDefaults, everything was fine until I deleted the plist file from the ~Library/Preferences/ directory. I thought the app should recreat it but it did not. When I debug I see that…
Bartosz Bialecki
  • 4,391
  • 10
  • 42
  • 64
16
votes
1 answer

[NSKeyedUnarchiver initForReadingWithData:]: data is NULL

Among the settings that I am saving to NSUserDefaults is a non-mandatory object that doesn't make sense to have an out-of-the-box default. Until the user sets a value for this object, the app generates the error "[NSKeyedUnarchiver…
Michael Mangold
  • 1,741
  • 3
  • 18
  • 32
15
votes
5 answers

Setting bundle default value won't set

I have an iOS application with a settings.bundle that handles various settings for my application with Switch toggle. I set default values in my root.plist file (using the DefaultValue property) to YES but any time the application launches on a…
iOS.Lover
  • 5,923
  • 21
  • 90
  • 162
15
votes
2 answers

How to clear UserDefaults for Xcode build of Mac app?

I'm trying to make sure my first-run code works properly, and so I'd like to clear the preferences file created by UserDefaults.standard.set calls. Where is it located, or is there at least a way to clear it out (other than writing it into my…
Joshua Breeden
  • 1,844
  • 1
  • 16
  • 29
15
votes
3 answers

Swift - Save Array of dictionaries into NSUserDefaults

I have an empty cart array -> var cart: [Dictionary] = [] And I have 2 screens, productsView and resumeView. Into the products view I have some products. When the user taps on a product, this goes to the the cart: cart = [[name: "A",…
pekpon
  • 761
  • 1
  • 10
  • 24
15
votes
5 answers

Swift - Saving highscore using NSUserDefaults

I'm using Swift to make a game. I want to save the users high score using NSUserDefaults. I know how to create a new NSUserDefaults variable in my AppDelegate file: let highscore: NSUserDefaults = NSUserDefaults.standardUserDefaults() But how do I…
user2397282
  • 3,798
  • 15
  • 48
  • 94
14
votes
3 answers

Why is NSUserDefaults unwilling to save my NSDictionary?

I'm using KVC to serialize an NSObject and attempt to save it to NSUserDefaults, which is giving me an Attempt to insert non-property value when I try to store my NSDictionary. Following are the properties of the object in question,…
Thomson Comer
  • 3,919
  • 3
  • 30
  • 32
14
votes
3 answers

How to Store Nested Arrays in @AppStorage for SwiftUI

@AppStorage was introduced in SwiftUI recently, it seems like an alternative to UserDefaults. I'm trying to make the @AppStorage able to store nested lists. For simple cases, you would do @AppStorage("selected") var selected = 0 I used this while…
Kelvin Jou
  • 271
  • 2
  • 12
14
votes
2 answers

Detect changes on NSUserDefaults with suiteName

I created appGroups and did set up a NSUserDefaults using that group. I have a observer for which is not triggered when the value is changed. let defaults = NSUserDefaults(suiteName: "group.daniesy.text")! defaults.addObserver(self,…
Dănuț Mihai Florian
  • 3,075
  • 3
  • 26
  • 44
14
votes
6 answers

NSMutableDictionary: mutating method sent to immutable object

The following code is returning an exception with the following error message "mutating method sent to immutable object" when attempting to removeObjectForKey NSMutableDictionary * storedIpDictionary = (NSMutableDictionary*)[[NSUserDefaults…
Remixed123
  • 1,575
  • 4
  • 21
  • 35
13
votes
2 answers

-[_SwiftValue encodeWithCoder:]: unrecognized selector sent to instance

Getting error when trying to utilize NSCoder Player.swift: class Player: NSObject, NSCoding { private var _playerName: String! private var _playerScore: Int! private var _playerColor: PlayerColor! //PlayerColor is an enum var…
Macness
  • 1,226
  • 2
  • 13
  • 24
13
votes
7 answers

Can you see the values of NSUserDefaults anywhere in the xcode debugger?

Can you see the values of NSUserDefaults naywhere in the xcode debugger? Just wondering if this is possible? Thanks, Nick
nickthedude
  • 4,925
  • 10
  • 36
  • 51