我目前正在尝试在我的应用程序中添加一个游戏保存功能。最终目标是获取保存到.plist文件中的~300个自定义对象,并在以后再次提取它们。我已经取得了一些进展,但是我对initWithCoder:有一些问题,我对我的技术也不太确定。
UIViewController正在使用以下代码来保存对象(例如,我只向字典中添加了2个对象):
//Saves the contents to a file using an NSMutableDictionary
-(IBAction)saveContents {
//Create the dictionary
NSMutableDictionary *dataToSave = [NSMutableDictionary new];
//Add objects to the dictionary
[dataToSave setObject:label.text forKey:@"label.text"];
[dataToSave setObject:territory forKey:@"territory"];
[dataToSave setObject:territory2 forKey:@"territory2"];
//Archive the dictionary and its contents and set a BOOL to indicate if it succeeds
BOOL success = [NSKeyedArchiver archiveRootObject:dataToSave toFile:[self gameSaveFilePath]];
//Handle success/failure here...
//Remove and free the dictionary
[dataToSave removeAllObjects]; dataToSave = nil;
}这成功地调用了encodeWithCoder:类中的Territory函数两次:
-(void)encodeWithCoder:(NSCoder *)aCoder {
NSLog(@"ENCODING");
[aCoder encodeObject:self.data forKey:@"data"];
[aCoder encodeInt:self.MMHValue forKey:@"MMHValue"];
[aCoder encodeObject:self.territoryName forKey:@"territoryName"];
//Continue with other objects
}当然,当我查看目录时,文件是存在的。--现在,我遇到的问题是:当运行以下函数时,initWithCoder:函数被成功地调用了两次。initWithCoder:函数中的日志输出它们应该输出的内容,但是UIViewController的loadContents函数中的日志返回0、null等。然而,标签的文本设置是正确的。
//Loads the contents from a file using an NSDictionary
-(IBAction)loadContents {
//Create a dictionary to load saved data into then load the saved data into the dictionary
NSDictionary *savedData = [NSKeyedUnarchiver unarchiveObjectWithFile:[self gameSaveFilePath]];
//Load objects using the dictionary's data
label.text = [savedData objectForKey:@"label.text"];
NSLog(@"%i, %i", [territory intAtKey:@"Key4"], [territory2 intAtKey:@"Key4"]);
NSLog(@"MMH: %i, %i", territory.MMHValue, territory2.MMHValue);
NSLog(@"NAME: %@, %@", territory.territoryName, territory2.territoryName);
//Free the dictionary
savedData = nil;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
NSLog(@"DECODING TERRITORY");
self.data = [aDecoder decodeObjectForKey:@"data"];
self.MMHValue = [aDecoder decodeIntForKey:@"MMHValue"];
self.territoryName = [[aDecoder decodeObjectForKey:@"territoryName"] copy];
//Continue with other objects
}
NSLog(@"DECODED INT: %i", self.MMHValue);
NSLog(@"DECODED NAME: %@", self.territoryName);
return self;
}几个小时以来,我一直在努力让这件事奏效,但没有结果。如果有人对此有任何见解,请帮帮我。另外,我不完全确定我的保存技术是否好(使用NSMutableDictionary存储对对象的引用,这样它就可以输出到一个文件)?谢谢!
发布于 2014-05-08 07:46:25
我阅读了苹果关于NSKeyedArchiver和NSKeyedUnarchiver的文档,以及一些关于这个主题的博客文章,我意识到自己做错了什么。在将对象编码到文件之后(通过使用键将它们添加到字典中,然后将它们保存到.plist中),当我尝试对它们进行解码时,我从未实际从新字典中提取值(保存刚刚解码的文件内容的字典)。这也解释了为什么加载label的文本,但territory和territory2没有加载。我将loadContents函数更改为以下内容,现在代码工作了:
-(IBAction)loadContents {
//Create a dictionary to load saved data into then load the saved data into the dictionary
NSLog(@"***** STARTING DECODE *****");
NSDictionary *savedData = [NSKeyedUnarchiver unarchiveObjectWithFile:[self gameSaveFilePath]];
NSLog(@"***** CALLING initWithCoder: ON OBJECTS *****");
NSLog(@"***** FINISHED DECODE *****");
//Load objects using the dictionary's data
label.text = [savedData objectForKey:@"label.text"];
territory = [savedData objectForKey:@"territory"];
territory2 = [savedData objectForKey:@"territory2"];
//Free the dictionary
savedData = nil;
}https://stackoverflow.com/questions/23488018
复制相似问题