简体   繁体   中英

iOS - plist file, nsuserdefaults or text data file

I am working on a mobile app which needs a lot of data. Simply put, the data will be for multiple languages and consist of all the words possible for that language. The app would start with only the English language and a lot of it's words. Then the user can choose to download more languages and their data.

I am trying to figure out the best way to read/save/update this data. Should I create a plist file with English data to start with and just keep adding more data as user downloads new languages? Or should I save all the data in nsuserdefaults? Or, should I just include a text file with all the data and parse it on the fly?

Suggestions?

ps: i understand that as this is a mobile app, file space and parsing time have to be considered

Please read my answer here: https://stackoverflow.com/a/7215501/832065

As said in that answer, all NSUserDefaults are stored together in one plist.

A plist and NSUserDefaults are basically the same, however NSUserDefaults should ONLY be used for saving preferences and not a big amount of data. So don't use NSUserDefaults!

I would consider saving this in a plist (NSDictionary). Like this you can have all data sorted in that file. Simply set the "words" (NSString I assume) as object, and the language as key.

NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithContentsOfFile:filePath];
[dict setObject:words forKey:language];
[dict writeToFile:filePath atomically:YES];

same for reading:

NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:filePath];
NSString *words = [dict objectForKey:language];

EDIT:

If each word is assigned to a number (you can just use NSArray) it doesn't differ much:

NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithContentsOfFile:filePath];
NSArray *words = [NSArray arrayWithObjects:allTheWords,nil];
[dict setObject:words forKey:language];
[dict writeToFile:filePath atomically:YES];

same for reading:

NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:filePath];
NSArray *words = [dict objectForKey:language];
NSString *word = [words objectAtIndex:numberAssignedToWord];

I hope this helps! :)

I would recommend using either an SQL database directly or Core Data. If you have a lot of data, you don't want to load that data completely into memory all the time. It is also easier to handle updates, changes or additions of data.

But your question is so general, and doesn't tell anything on what you actually want to do with the data, how large the data is in KByte or MByte, it is hard to give any good answer.

Writing a lot of data into a text file, user defaults, plist or something similar, doesn't seem the right choice. Using Core Data would be the default way to do such things on iOS.

I thing the easiest way would be to handle the data in a NSMutableArray and store it within the application using NSUserDefaults .

You can handle your data within a NSMutableArray , like so:

NSMutableArray *yourDataStuff = [[NSMutableArray alloc] init];
[yourDataStuff addObject:@"Your data 1"];
[yourDataStuff addObject:@"Your data 2"];
[yourDataStuff addObject:@"Your data 3, etc.."];

Then you can store it using NSUserDefaults , like so:

[[NSUserDefaults standardUserDefaults] setObject:yourDataStuff forKey:@"myData"];

And read your data, like so:

yourDataStuff = [[NSUserDefaults standardUserDefaults] objectForKey:@"myData"];

I think you cannot save large data in the NSUserDefault , and you maybe can not do IO with plist , i had done this , when you read or save , you must read in the whole .plist , and you may reveive memory warning , or crash. you can use C (fread,fwrite) write the data in a .txt , and you may read and write with data stream.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM