我试图保存一个名为queueArray的NSMutableArray
,以便在应用程序退出后再次加载它。我使用了几个教程让我走,这是我已经提出的代码。问题似乎是“initWithCoder
”和“encodeWithCoder
”没有被调用,显示为没有NSLog
调用并且没有在断点处停止。我已经加入了NSCoding
协议.h文件,我知道queueArray不是零,它包含MPMediaItem
秒。以下是我用来尝试保存和加载阵列的一些代码:iOS/iPhone SDK:initWithCoder和encodeWithCoder不被调用
-(IBAction)saveQueuePressed {
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [rootPath stringByAppendingPathComponent:@"queueArray.archive"];
//should cause encodeWithCoder to be called
[NSKeyedArchiver archiveRootObject:queueArray toFile:filePath];
}
-(IBAction)loadQueuePressed {
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [rootPath stringByAppendingPathComponent:@"queueArray.archive"];
//should cause initWithCoder to be called
queueArray = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
}
-(void)encodeWithCoder:(NSCoder *)coder {
NSLog(@"encodeWithCoder");
[coder encodeObject:queueArray forKey:@"savedQueueArray"];
}
-(id)initWithCoder:(NSCoder *)decoder {
NSLog(@"initWithCoder");
queueArray = [decoder decodeObjectForKey:@"savedQueueArray"];
return self;
}
任何帮助将不胜感激!
使用“101010 “按钮来格式化代码。 – 2010-09-20 17:06:58