How to save game data for my iOS Cocos2d game?

I have a lot of different data related to the levels, and therefore I need to save what will be saved even if the player turns off / off the phone, restarts the device, leaves the game, etc. Mostly persistent data. I considered many of my options, but did not find a simple, understandable method for what I needed, and I would like someone to help me and give a clear example of how to implement the basis of the best method for my needs.

I reviewed the following NSUSerDefaults (Obviously not the best, as it is for preference, so I understand) NSCoder / NSKeyedArchiver (cannot find a clear way to just save a simple data type from one single class that saves all the data as properties) SQLite3 ( completely lost)

Any help and guidance would be greatly appreciated.

The types of data that I need to save and easily retrieve throughout my program are ... NSStrings, NSArrays, Ints, Bools.

Thanks for the help and I hope to get a clear answer!

+4
source share
1 answer

Of course, there is nothing wrong with saving NSUserDefaults, but if you want to save your properties to disk, I collected some code for you to save to a .plist file, and then later extract it. You can also find it in gist .

Preservation

// We're going to save the data to SavedState.plist in our app documents directory NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString *plistPath = [rootPath stringByAppendingPathComponent:@"SavedState.plist"]; // Create a dictionary to store all your data NSMutableDictionary *dataToSave = [NSMutableDictionary dictionary]; // Store any NSData, NSString, NSArray, NSDictionary, NSDate, and NSNumber directly. See "NSPropertyListSerialization Class Reference" for more information. NSString *myString = @"Hello!" [dataToSave setObject:myString forKey:@"MyString"]; // Wrap primitives in NSValue or NSNumber objects. Here are some examples: BOOL someBool = YES; NSNumber *boolValue = [NSNumber numberWithBool:someBool]; [dataToSave setObject:boolValue forKey:@"SomeBoolValue"]; int someInteger = 99; NSInteger *integerValue = [NSNumber numberWithInteger:someInteger]; [dataToSave setObject:integerValue forKey:@"SomeIntegerValue"]; // Any objects that conform to NSCoding can be archived to an NSData instance. In this example, MyClass conforms to NSCoding. MyClass *someObject = [[MyClass alloc] init]; NSData *archivedStateOfSomeObject = [NSKeyedArchiver archivedDataWithRootObject:someObject]; [dataToSave setObject:archivedStateOfSomeObject forKey:@"SomeObject"]; // Create a serialized NSData instance, which can be written to a plist, from the data we've been storing in our NSMutableDictionary NSString *errorDescription; NSData *serializedData = [NSPropertyListSerialization dataFromPropertyList:dataToSave format:NSPropertyListXMLFormat_v1_0 errorDescription:&errorDescription]; if(serializedData) { // Write file NSError *error; BOOL didWrite = [serializedData writeToFile:plistPath options:NSDataWritingFileProtectionComplete error:&error]; NSLog(@"Error while writing: %@", [error description]); if (didWrite) NSLog(@"File did write"); else NSLog(@"File write failed"); } else { NSLog(@"Error in creating state data dictionary: %@", errorDescription); } 

Loading

 // Fetch NSDictionary containing possible saved state NSString *errorDesc = nil; NSPropertyListFormat format; NSString *plistPath; NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; plistPath = [rootPath stringByAppendingPathComponent:@"SavedState.plist"]; NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath]; NSDictionary *unarchivedData = (NSDictionary *)[NSPropertyListSerialization propertyListFromData:plistXML mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format errorDescription:&errorDesc]; // If NSDictionary exists, look to see if it holds a saved game state if (!unarchivedData) { NSLog(@"Error reading plist: %@, format: %d", errorDesc, format); } else { // Load property list objects directly NSString *myString = [unarchivedData objectForKey:@"MyString"]; // Load primitives NSNumber *boolValue = [unarchivedData objectForKey:@"SomeBoolValue"]; BOOL someBool = [boolValue boolValue]; NSNumber *integerValue = [unarchivedData objectForKey:@"SomeIntegerValue"]; BOOL someBool = [integerValue integerValue]; // Load your custom objects that conform to NSCoding NSData *someObjectData = [unarchivedData objectForKey:@"SomeObject"]; MyClass *someObject = [NSKeyedUnarchiver unarchiveObjectWithData:someObjectData]; } 

For further reading, see the Archive Programming and Serialization Guide .

+9
source

Source: https://habr.com/ru/post/1446830/


All Articles