Should I use NSUserDefaults or a plist to store data?
Solution 1
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.
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"];
Create the array (I am assuming the strings are string1, ...)
NSArray *stringsArray = [[NSArray alloc] arrayWithObjects: string1, string2, string3, nil];
Write it to file
[stringsArray writeToFile:stringsPlistPath atomically:YES];
To read the plist:
Find the documents directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *stringsPlistPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"Strings.plist"];
Read it in:
NSArray *stringsArray = [NSArray arrayWithContentsOfFile:stringsPlistPath];
Solution 2
iOS ultimately stores all NSUserDefaults
data to a plist file. So it will not affect the performance if that is your concern. I personally prefer using NSUserDefaults
for small data and plist for a relatively large set of data.
Note: Never store any sensitive information in NSUserDefaults
as anyone can see that data.
Solution 3
If you are storing 10-20 strings and are looking for not too many lines of code, core data is certainly much too much overhead. I recommend going with the plist. Not a lot of code:
NSURL *plistURL = [[NSBundle mainBundle] URLForResource:@"MyStrings" withExtension:@"plist"];
NSArray *stringArray = [NSArray arrayWithContentsOfURL:plistURL];
Solution 4
NSUserDefaults will store the user preferences into a file into the Library/Preferences folder. In theory it serves only to store some application/user properties.
Plist file are usefull to manage a single file. If you need to manage more you should use the Coredata. There is no restriction about the size of the plist file. Otherwise you have to be careful with plist file because when you need to save or read it the entire contents of the file will be load into memory.
Solution 5
Using .plist
- Create a plist using Xcode
Write a value to plist
NSURL *plistURL = [[NSBundle mainBundle] URLForResource:@"settings" withExtension:@"plist"];
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithContentsOfURL:plistURL];
[dict setValue:@"value" forKey:@"key"];
[dict writeToURL:plistURL atomically:YES];
Read a value from plist
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithContentsOfURL:plistURL];
NSString *myValue = [dict valueForKey:@"key"];
Sheehan Alam
iOS, Android and Mac Developer. i can divide by zero.
Updated on July 19, 2022Comments
-
Sheehan Alam almost 2 years
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.