2010-09-20 61 views
2

我试图保存一个名为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; 

} 

任何帮助将不胜感激!

+0

使用“101010 “按钮来格式化代码。 – 2010-09-20 17:06:58

回答

1

当您归档/取消归档响应它们的对象时,会调用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]; 
} 

但如果你只是想保存的阵列,我想你可以只使用saveQueuePressedloadQueuePressed,我不要以为你所需要的encode/init WithCoder:方法

更新: 也许你的路径是不正确的。 尝试

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

我已经改变的代码://保存 的NSString * ROOTPATH = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)objectAtIndex:0]; 的NSString *文件路径= [NSString的stringWithFormat:@ “%@/queueArrayFile”,ROOTPATH]; \t [queueArray将writeToFile:文件路径原子:NO]; \t //检索 的NSMutableArray * retrievedArray = [[NSMutableArray里的alloc] initWithContentsOfFile:文件路径]。当我找回它\t 但是,它仍然无法正常运转的数组为空。我不认为我能够保存MSMutableArray充满MPMediaItems的。我该怎么办? – Craig 2010-09-20 19:24:41

+0

也许路径不好? – filipe 2010-09-20 20:27:38

0

菲利普是正确的!你的评论说你还没有使用他的方法。

我也有这个问题。从字典中的原子写入方法的keyedArchiver开关固定它,幸运的是我只需要改变一两件事,那表示将writeToFile行:现在是存档功能。

现在我的程序正在工作。出于某种原因,即使在响应NSCoding时,自定义对象也没有被编码并破坏我的字典。这是iOS的错误吗?我读过相当数量的苹果手册的,但我也看到了相当多的错别字和失踪的信息(例如,尝试没有视频MKMapRect功能来解释它们),或核心动画引用运行循环,你学会前线程,我可以继续,在Quartz中完成一半的句子...所以是的,我已经阅读了手册,这让我感到困惑,我们必须在某个时候获得更开放的iOS SDK,希望