Cocoa touch iOS/iPhone SDK:未调用initWithCoder和encodeWithCoder

Cocoa touch iOS/iPhone SDK:未调用initWithCoder和encodeWithCoder,cocoa-touch,ios,Cocoa Touch,Ios,我正在尝试保存一个名为queueArray的NSMutableArray,以便在退出应用程序后可以再次加载它。我用了一些教程让我开始,这是我的代码。问题似乎是“initWithCoder”和“encodeWithCoder”没有被调用,表现为没有NSLog调用,也没有在断点处停止。我已将NSCoding协议添加到.h文件中,并且我知道queueArray不是nil,它包含MPMediaItems。下面是我用来尝试保存和加载数组的一些代码: -(IBAction)saveQueuePressed

我正在尝试保存一个名为queueArray的
NSMutableArray
,以便在退出应用程序后可以再次加载它。我用了一些教程让我开始,这是我的代码。问题似乎是“
initWithCoder
”和“
encodeWithCoder
”没有被调用,表现为没有
NSLog
调用,也没有在断点处停止。我已将
NSCoding
协议添加到.h文件中,并且我知道queueArray不是nil,它包含
MPMediaItem
s。下面是我用来尝试保存和加载数组的一些代码:

-(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;

}

任何帮助都将不胜感激

存档/取消存档响应它们的对象时,将调用
encodeWithCoder:
initWithCoder
方法。据我所知,类中有这些方法,但实际存档的对象(
queueArray
)不是该类的实例,而是一个
NSMutableArray

如果确实要保存整个对象,可以将saveQueue方法更改为

-(IBAction)saveQueuePressed {

    NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *filePath = [rootPath stringByAppendingPathComponent:@"queueArray.archive"];

    // saving the array shouldn't, but saving the self object
    //should cause encodeWithCoder to be called:
    [NSKeyedArchiver archiveRootObject:self toFile:filePath];
}
但是如果您只想保存数组,我想您可以使用
saveQueuePressed
loadQueuePressed
,我认为您不需要
encode/initwithcoder:
方法

更新: 也许你的道路不对。 试一试


菲利佩是对的!你的评论说你仍然没有使用他的方法

我也有这个问题。从字典的原子写入方法切换到keyedArchiver修复了它,幸运的是,我只需要更改一件事,写writeToFile:的行现在是存档函数


现在我的程序开始工作了。出于某种原因,即使在响应NSCoding时,自定义对象也没有被编码,并破坏了我的字典。这是iOS的错误吗?我已经阅读了相当多的苹果手册,但我也看到了相当多的打字错误和信息缺失(例如,在没有视频解释的情况下尝试MKMapRect函数),或者在学习线程之前参考Run循环的核心动画,我可以继续说,用Quartz完成了一半的句子。。。因此,是的,我已经阅读了手册,这让我感到困惑,我们必须在某个时候获得一个更开放的iOS SDK,希望使用“101010”按钮格式化代码。我已将代码更改为://以保存NSString*rootPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)objectAtIndex:0];NSString*文件路径=[NSString stringWithFormat:@“%@/queueArrayFile”,根路径];[queueArray writeToFile:文件路径原子:否];//检索NSMutableArray*retrievedArray=[[NSMutableArray alloc]initWithContentsOfFile:filePath];但是,当我检索数组时,它仍然不工作,因为数组是空的。我认为我无法保存一个装满MPMediaItems的MSMutableArray。我该怎么办?
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [[rootPath stringByAppendingPathComponent:@"queueArray.archive"] stringByExpandingTildeInPath]];