开发者

Saving lots of dictionaries and arrays

My game comes with lots of .plist files containing many dictionaries, arrays, etc. They are about each party member's stats, info, etc.

A singleton is in charge of reading all those files and provide the necessary data for my game to run.

The game, at many points, w开发者_运维知识库ill edit such values from the singleton, like a hero's level, stats, etc.

Eventually, the player will be given the option to "save" the game. My original idea was to basically tell the singleton to "overwrite" the .plist files within the project with the new edited data (the new data might have changes in stat entries etc, or even brand-new dictionaries representing new party members etc) and done.

But, I don't know how to do that, or even if it is possible.

I heard about NSUserDefaults, but I don't know if it suitable for what I am aiming, considering that the amount of dictionaries and arrays will be quite big, and that it is possible to keep adding up more dictionaries and arrays in the future. Imagine that the party can have as many members as you wish, for instance.

*Note: there are no multiple save files/profiles. When you press save, it saves, without asking anything else.


NSUserDefaults is not suitable for any substantial amount of data. Just write the plist to the documents directory, specify atomic:YES to insure the entire file is written.

- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)flag

If YES, the array (or dictionary) is written to an auxiliary file, and then the auxiliary file is renamed to path. If NO, the array is written directly to path. The YES option guarantees that path, if it exists at all, won’t be corrupted even if the system should crash during writing.

This method exists for both NSArray and NSDictionary,

Obtain the document directory:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:fileName];
BOOL status = [NSDictionary writeToFile:filePath atomically:YES];

NSDictionary could have ten NSArray depending on the top element of the plist.

Consider using CoreData.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜