Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 implement.

I'd like to add that these string values will be added/removed by the user.

like image 906
Sheehan Alam Avatar asked Aug 14 '11 18:08

Sheehan Alam


People also ask

How much data can you store in NSUserDefaults?

It appears the limit is the maximum file size for iOS (logically), which is currently 4GB: https://discussions.apple.com/thread/1763096?tstart=0. The precise size of the data is circumscribed by the compiler types (NSData, NSString, etc.) or the files in your asset bundle.

Is NSUserDefaults secure?

Because NSUserDefaults stores all data in an unencrypted . plist file, a curious person could potentially view this data with minimal effort. That means that you should never store any type of sensitive data inside NSUserDefaults.

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. These methods are described in Setting Default Values.

Should I use UserDefaults?

The defaults system is great and I recommend using it but only when it is appropriate. As I mentioned earlier, don't use it to store large data sets or other types of data it isn't designed for, such as image data. Remember that the UserDefaults class needs to load it from disk and keeps it in memory.


1 Answers

I am assuming an array, but it will work with dictionaries too.

Userdefaults, Core Data and Plists can all be read/write but if you use a plist you need to pay attention in what dir you put it. See the plist part down below.

Core Data I think it's way too much overkill, it's just strings. It's supposed to be used when you want to persist more complex objects.

NSUserDefaults:

It's pretty fast and easy to do, though it's supposed to store only user settings. To write them to the userdefaults:

NSArray *stringsArray = [[NSArray alloc] arrayWithObjects: string1, string2, string3, nil]; [[NSUserDefaults standardUserDefaults] setObject:stringsArray forKey:@"MyStrings"]; [[NSUserDefaults standardUserDefaults] synchronize]; 

To read the from the userdefaults:

NSArray *stringsArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"MyStrings"]; 

Plist:

If your strings are going to be modified you will need to write and read a plist but you cant't write into your app's resources.

  1. To have a read/write plist first find the documents directory

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *stringsPlistPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Strings.plist"]; 
  2. Create the array (I am assuming the strings are string1, ...)

    NSArray *stringsArray = [[NSArray alloc] arrayWithObjects: string1, string2, string3, nil]; 
  3. Write it to file

    [stringsArray writeToFile:stringsPlistPath atomically:YES]; 

To read the plist:

  1. Find the documents directory

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *stringsPlistPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Strings.plist"]; 
  2. Read it in:

    NSArray *stringsArray = [NSArray arrayWithContentsOfFile:stringsPlistPath]; 
like image 70
doppioslash Avatar answered Oct 02 '22 12:10

doppioslash