我試圖在iOS應用程序中保存一些數據。我使用下面的代碼:如何停止覆蓋數據
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"yourPlist.plist"];
//inserting data
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setValue:categoryField.text forKey:@"Category"];
[dict setValue:nameField.text forKey:@"Name"];
[dict setValue:eventField.text forKey:@"Event"];
NSMutableArray *arr = [[NSMutableArray alloc] init];
[arr addObject:dict];
[arr writeToFile: path atomically:YES];
//retrieving data
NSMutableArray *savedStock = [[NSMutableArray alloc] initWithContentsOfFile: path];
for (NSDictionary *dict in savedStock) {
NSLog(@"my Note : %@",dict);
}
然而NSLog的顯示我只有最後一個數據......我想,我正在改寫here..I不能明白爲什麼,但!
如何繼續保存字典中的字典而不覆蓋?有任何想法嗎?
我已經給你以前的[問題](http://stackoverflow.com/q/16480807/767730)[回覆](http://stackoverflow.com/a/16481217/767730),它會照顧覆蓋。 Incase你沒有檢查。 – Anupdas 2013-05-10 15:17:33
我知道,我強烈重新考慮將接受的答案更改爲您的答案。該代碼似乎更容易,這就是爲什麼我選擇它。 – LolaEnaMilo 2013-05-10 15:25:52
我不是要求你接受它,另一個很容易,因爲它沒有考慮到所有覆蓋,易用性,封裝的存儲等。我建議你試試看。您可以將plist保存爲字典,將模型對象轉換爲字典,並在回調時進行反轉。 – Anupdas 2013-05-10 15:30:17