2014-05-18 81 views
2

我知道您必須先從plist檢索數據,添加數據並將整個內容重寫到plist文件。將數據保存到xcode中plist

但我在這裏的代碼不是附加,但每次我調用這個方法刷新。

- (void) saveDataToPlist { 
    // Retrieve path and filename of the plist file 
    NSString *myColorsListFile = [self dataFilePath]; 

    NSMutableArray *innerArray1; 
    NSString *error; 

    UserGivenColorHexString = HexTextField.text; 
    NSMutableDictionary *rootObj = [NSMutableDictionary dictionaryWithCapacity:100]; 

    if ([[NSFileManager defaultManager] fileExistsAtPath:myColorsListFile]) { 
     // File exists 
     rootObj = [[NSMutableDictionary alloc] initWithContentsOfFile:myColorsListFile]; 


     [innerArray1 addObjectsFromArray:[NSMutableArray arrayWithContentsOfFile:myColorsListFile]]; 
     [innerArray1 addObject:UserGivenColorName]; 

    } else { 
     // Create file 
     rootObj = [[NSMutableDictionary alloc] init]; 

     innerArray1 = [NSMutableArray arrayWithObjects: UserGivenColorName, nil]; 
     [rootObj setObject:innerArray1 forKey:@"ColorName"]; 
    } 

    id plist = [NSPropertyListSerialization dataFromPropertyList:(id)rootObj format:NSPropertyListXMLFormat_v1_0 errorDescription:&error]; 

    [plist writeToFile:myColorsListFile atomically:YES]; 
} 

回答

3

似乎有成爲一堆事項:關鍵的問題是,你是不是設置innerArray1變量如果plist中被發現,所以你嘗試加入到這將是徒勞的。與plist中的內容加載rootObj後,你必須獲得對位於關鍵ColorName數組的引用:

innerArray1 = [rootObj objectForKey:@"ColorName"]; 

然後代碼將工作。

無關的這種立即ISSE,還有其他一些小問題:

  • 您正在閱讀與myColorsListFile的plist字典,但是你也嘗試了這樣的內容添加爲數組innerArray1 。後者myColorsListFile的使用讓我覺得這是該plist的無效使用。我會刪除該行。

  • 看來你設置UserGivenColorHexString,但然後保存UserGivenColorName(我可能也更改爲一個局部變量,以避免其他地方意想不到的後果)。我假設你打算爲這兩個使用相同的變量。

  • 您還在實例化隨後丟棄的可變字典。

  • 我可能會建議符合Cocoa Naming Conventions(以小寫字母開始變量)。我也推測hexTextField是一個真正的財產。

  • 從「我找到ColorName數組」邏輯中分離出「文件存在」邏輯可能會更健壯一些。

  • 由於您正在捕獲plist創建失敗錯誤,因此如果遇到它,您還可以將其記錄下來。

  • documentation告訴我們,dataFromPropertyList現在已經過時:

    特別注意事項

    此方法已過時,很快就會過時。改爲使用dataWithPropertyList:format:options:error:

所以,你可能喜歡的東西最終會:

- (void) saveDataToPlist { 
    // Retrieve path and filename of the plist file 
    NSString *myColorsListFile = [self dataFilePath]; 

    NSMutableArray *innerArray1; 
    NSError *error; 

    NSString *userGivenColorHexString = HexTextField.text; 
    NSMutableDictionary *rootObj; 

    if ([[NSFileManager defaultManager] fileExistsAtPath:myColorsListFile]) { 
     rootObj = [[NSMutableDictionary alloc] initWithContentsOfFile:myColorsListFile]; 
    } else { 
     rootObj = [[NSMutableDictionary alloc] init]; 
    } 

    innerArray1 = [rootObj objectForKey:@"ColorName"]; 

    if (innerArray1) { 
     [innerArray1 addObject:userGivenColorHexString]; 
    } else { 
     innerArray1 = [NSMutableArray arrayWithObjects: userGivenColorHexString, nil]; 
     [rootObj setObject:innerArray1 forKey:@"ColorName"]; 
    } 

    id plist = [NSPropertyListSerialization dataWithPropertyList:(id)rootObj format:NSPropertyListXMLFormat_v1_0 options:0 error:&error]; 
    if (!plist) { 
     NSLog(@"dataFromPropertyList error: %@", error); 
    } 

    [plist writeToFile:myColorsListFile atomically:YES]; 
} 
+0

羅布,我沒有足夠的聲譽投票您的文章/解決方案UP。但如果我能做到這一點,我會做到100倍。您的解決方案完美運行,並且您的所有評論都將受到讚賞謝謝! – user3477156