2012-11-18 146 views
4

我有一大堆相關的水平雜項數據,並且這樣的,我需要保存,將保存即使玩家打開自己的手機/關閉,重新啓動設備,退出遊戲等,基本上持久性數據。我已經看了很多我的選擇,但還沒有找到我需要什麼,並希望有人請幫助我,並給出如何實現我的需求的最佳方法的基礎上一個明顯的例子,一個簡單,明確的方法。如何保存遊戲數據爲我的iOS的Cocos2D遊戲?

我已經看着下面 NSUserDefaults的(顯然不是最好的,因爲它的喜好,讓我明白了) NSCoder /的NSKeyedArchiver(想不通一個清晰的方式來只保存簡單數據類型,從1個單班所有的數據保存在作爲屬性) SQLite3的(完全喪失)

任何幫助和方向將不勝感激。

的數據類型我需要保存,輕鬆地在我的程序訪問是... NSString的,NSArrays,整型,布爾變量。

謝謝你的幫助,我希望能得到一個明確的答案!

+0

'NSUserDefaults'對於這一點來說是完美的 - 它絕對是您存儲數據量非常少的正確存儲選項。 – MaxGabriel

+0

謝謝。我也結束了持續NSUserDefaults的爲它的簡單性和由於還真是不多我存儲。其他保存方式的代碼開銷對我的數據的簡單程度沒有意義。 –

回答

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

對於進一步閱讀,檢查出Archives and Serialization Programming Guide

+0

這對我有用。謝謝。 –