Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to store NSDictionary in NSUserdefaults and how can I retrieve from it

Tags:

iphone

I am getting some info from web server like this.

970,
Aditya2,
B,
JNTU1,
"[email protected]"

I need to store these values and I need to retrieve it from there where I want.

I found NSDictionary is correct for this.

I need to save these values in NSDictionary.

How can I do this?

like image 414
Mahesh Babu Avatar asked Dec 30 '10 12:12

Mahesh Babu


People also ask

Where is NSUserDefaults stored?

All the contents saved by NSUserDefaults is saved inside a plist file that can be found under Library -> Preferences -> $AppBundleId.

What types can you store natively in NSUserDefaults?

Types stored in NSUserDefaultsAny . plist type can be stored by NSUserDefaults . These types are NSString(String) , NSArray(Array) , NSDictionary(Dictionary) (for both NSArray and NSDictionary types their contents must be property list objects), NSNumber(Int, Float, Double, Boolean) , NSDate and NSData .

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.

How much data can you store in UserDefaults?

Currently, there is only a size limit for data stored to local user defaults on tvOS, which posts a warning notification when user defaults storage reaches 512kB in size, and terminates apps when user defaults storage reaches 1MB in size.


1 Answers

To store and then receive:

[[NSUserDefaults standardUserDefaults] setObject:myDictionary forKey:@"dictionaryKey"];
//...
NSDictionary * myDictionary = [[NSUserDefaults standardUserDefaults] dictionaryForKey:@"dictionaryKey"];

Here's a link to the NSUserDefaults Class Reference

like image 120
thelaws Avatar answered Sep 20 '22 18:09

thelaws