2010-09-20 23 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,希望