4

我有一堆与关卡相关的杂项数据,因此我需要保存,即使玩家关闭/打开手机、重新启动设备、退出游戏等,这些数据也会被保存。基本上是持久数据。我查看了很多选项,但没有找到一种简单、清晰的方法来满足我的需求,希望有人能帮助我,并给出一个清晰的例子来说明如何实现满足我需求的最佳方法的基础。

我查看了以下 NSUSerDefaults (显然不是最好的,因为它是为了偏好,所以我理解) NSCoder / NSKeyedArchiver (无法找到一种明确的方法来从 1 个单个类中保存简单数据类型,所有数据都已保存作为属性)SQLite3(完全丢失)

任何帮助和指导将不胜感激。

我需要在整个程序中保存和轻松访问的数据类型是...... NSStrings、NSArrays、Ints、Bools。

感谢您的帮助,我希望得到一个明确的答案!

4

1 回答 1

9

保存到 NSUserDefaults 肯定没有错,但是如果您想将属性保存到磁盘,我已经为您整理了一些代码,用于保存到 .plist 文件,然后再检索它。你也可以在这个gist中找到它。

保存

// We're going to save the data to SavedState.plist in our app's 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);
}

正在加载

// 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];
}

如需进一步阅读,请查看档案和序列化编程指南

于 2012-11-19T00:25:44.053 回答