Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving NSNumber in NSUserDefaults

Using currentIndex as NSNumber and saving NSNumber in NSUserDefaults.

Using currentIndex to store bookmarked page in NSUserDefaults

And this is how I am saving currentIndex in NSUserdefaults:

 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
 [defaults setInteger:currentIndex forKey:@"currentIndex"];
 [defaults synchronize];

If this is the right way of saving currentIndex in NSUserdefaults then how can i retrieve saved currentIndex in NSUserdefaults.

Thanks for help.

like image 973
user1120133 Avatar asked Apr 12 '14 22:04

user1120133


People also ask

How to save data in NSUserDefaults in objective c?

Saving to NSUserDefaults : Basically, all you have to do is load NSUserDefaults, and then tell it to save a value to a specific key. Here is a simple example of writing an integer to NSUserDefaults: NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

How do I save an array in NSUserDefaults in Objective C?

You can save your mutable array like this: [[NSUserDefaults standardUserDefaults] setObject:yourArray forKey:@"YourKey"]; [[NSUserDefaults standardUserDefaults] synchronize]; Later you get the mutable array back from user defaults. It is important that you get the mutable copy if you want to edit the array later.

What types can you store natively in NSUserDefaults?

Storing Default Objects The NSUserDefaults class provides convenience methods for accessing common types such as floats, doubles, integers, Boolean values, and URLs.

Where NSUserDefaults data is saved?

The user's defaults database is stored on disk as a property list or plist. A property list or plist is an XML file. At runtime, the UserDefaults class keeps the contents of the property list in memory to improve performance. Changes are made synchronously within the process of your application.


2 Answers

The title says that you want to store a NSNumber, you should do that in this way:

obj-c

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:nsnumberObject forKey:@"nsNumberStored"];

swift

let defaults = NSUserDefaults.standardUserDefaults()
defaults.setObject(nsnumberObject, forKey:"nsNumberStored")

And then to fetch the NSNumber do this:

obj-c

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSNumber *nsNumberFetched = [defaults objectForKey:@"nsNumberStored"];

swift

let defaults = NSUserDefaults.standardUserDefaults()
defaults.objectForKey("nsNumberStored")

You can read the official documentation here Class reference

Let me know if it helps you.

like image 116
Gabriel Goncalves Avatar answered Nov 04 '22 03:11

Gabriel Goncalves


First of all, setInteger: forKey: is used to store an int, not a NSNumber. If you want to store a NSNumber, user setObject: forKey:.

In order to retrieve your stored data, use objectForKey: (or integerForKey: instead if you would rather use int's).

like image 5
WolfLink Avatar answered Nov 04 '22 02:11

WolfLink