簡體   English   中英

在iPhone中使用plist時應用程序崩潰問題

[英]Application crash problem when using plist in iPhone

我使用了NSMutable字典和NSMutable數組。 將使用NSMutable字典的NSArray從plist(文檔目錄)存儲和檢索數據。

編輯:

我在滾動視圖中有六個按鈕,單擊按鈕后,將使用XML解析在表視圖中顯示數據。 我已將數據放入plist並從plist檢索並顯示在表格視圖中。 在表格視圖中,我有一個按鈕作為頁腳視圖。 單擊該按鈕時,將解析下一項並在表視圖中顯示。 下一個解析內容將添加到先前的內容中。 我已經使用NSMutable字典將數據存儲到plsit中。

這是我的代碼

返回數組是返回解析的詳細信息和。

  -(void)callFromSecondaryThread:(NSMutableArray *)returnArray{

     if([returnArray count] > 0)
     {
         for(NSMutableArray *arr in returnArray)
         {
             //[copyArray addObjectsFromArray:returnArray];  
             //[copyArray addObject:returnArray];   
             //[ ???? ??? ??????? ?????? ] 
         } 
               [self.table reloadData];
     }

//第一次將內容保存在plist中並從plist加載內容並顯示在表格視圖中

 -(void)startParsingBasedOnSelectedTabValue:(NSString *)strValue {//button value

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    if (![self checkFileIsThere:tagString]) { //checks the plist is available or not, if its not available, then create a new file. i have created separate plist file for each buttons.

    if([copyArray count] > 0)
    {
        [copyArray removeAllObjects];
    }

    feedDictionary = [[NSMutableDictionary alloc]init];

    //send url for parsing [self parsingMethod];

    [feedDictionary setObject:copyArray forKey:tagString];

    [feedDictionary writeToFile:[self saveFilePath:tagString] atomically:NO];

    [feedDictionary release];

    } else{

             NSString *stringPtah = [self saveFilePath:tagString];

             NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithContentsOfFile:stringPtah];

             NSMutableArray *arrArray = [dictionary objectForKey: tagString];

             copyArray = [arrArray copy];
                   }  
            [self.table performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
            [pool drain];
       }


     //On clicks the next 10 details button, 

 -(void) details:(NSString *) strpageCount
 {

    if ([self checkFileIsThere:tagString]) {

    [[NSFileManager defaultManager]  removeItemAtPath:[self saveFilePath:tagString] error:NULL];  // remove the old plist here

     NSMutableDictionary *feedDictionary1 = [[NSMutableDictionary alloc]init];

     [self parsingMehods];

     [feedDictionary1 setValue:copyArray forKey:tagString];

     [feedDictionary1 writeToFile:[self saveFilePath:tagString] atomically:NO];

     [pageDictionary setValue:pString forKey:tagString];

     [feedDictionary1 release];

     [self.table reloadData]; // After writing the file in the plist, i just reload the table data and displayed the contents in the table view. 

    }
 }

但是我有時只能成功地將數據添加到copyArray中。 這意味着,我在加載視圖后單擊了下一步按鈕,它工作正常。 但是,如果我移動了下一個按鈕並訪問了該視圖,然后回到第一個視圖並單擊了下一個按鈕,我的應用程序將崩潰。

我可以在returnArray中獲取數據,copyArray是我的主數組,它用於顯示內容(CellforRowatindex)。ReturnArray是一個可變數組,它包含標題,日期,圖像等,

單擊(下10個詳細信息)按鈕后,我刪除了舊的plist並使用新內容創建了一個新plist。(例如:第一個plist在復制數組中有10個內容並返回4個新內容。它將添加14復制數組中的內容並顯示在表格視圖中)。 第一次加載我的應用程序時,單擊下一步按鈕,將成功添加陣列。 但是我訪問了其余的按鈕和詳細信息,然后返回視圖並單擊下一個按鈕,我崩潰了我的應用程序並得到了此異常,

     Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFArray removeObjectAtIndex:]: mutating method sent to immutable object' 

我花了很多時間解決這個問題,直到沒有找到解決方案。

請提及我在代碼中做錯了什么。

請指導我?

謝謝!

copyArray = [arrArray copy]; 應該是copyArray = [arrArray mutableCopy]; 您收到錯誤,因為copyArray是不可變的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM