我試圖保存一個名爲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