2013-05-10 47 views
2

我試圖在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不能明白爲什麼,但!

如何繼續保存字典中的字典而不覆蓋?有任何想法嗎?

+0

我已經給你以前的[問題](http://stackoverflow.com/q/16480807/767730)[回覆](http://stackoverflow.com/a/16481217/767730),它會照顧覆蓋。 Incase你沒有檢查。 – Anupdas 2013-05-10 15:17:33

+0

我知道,我強烈重新考慮將接受的答案更改爲您的答案。該代碼似乎更容易,這就是爲什麼我選擇它。 – LolaEnaMilo 2013-05-10 15:25:52

+0

我不是要求你接受它,另一個很容易,因爲它沒有考慮到所有覆蓋,易用性,封裝的存儲等。我建議你試試看。您可以將plist保存爲字典,將模型對象轉換爲字典,並在回調時進行反轉。 – Anupdas 2013-05-10 15:30:17

回答

2

因爲你是一個模型對象,它會更好,如果你有保存,刪除, findAll,findByUniqueId內置的一種邏輯。將使模型對象的工作非常簡單。

@interface Note : NSObject 

@property (nonatomic, copy) NSString *category; 
@property (nonatomic, copy) NSString *name; 
@property (nonatomic, copy) NSString *event; 

- (id)initWithDictionary:(NSDictionary *)dictionary; 

/*Find all saved notes*/ 
+ (NSArray *)savedNotes; 

/*Saved current note*/ 
- (void)save; 

/*Removes note from plist*/ 
- (void)remove; 

保存筆記

Note *note = [Note new]; 
note.category = ... 
note.name = ... 
note.event = ... 

[note save]; 

從保存的列表

//Find the reference to the note you want to delete 
Note *note = self.savedNotes[index]; 
[note remove]; 

刪除找到所有保存的筆記

NSArray *savedNotes = [Note savedNotes]; 

Source Code

0

您需要先讀入數據,然後將新字典APPEND添加到舊字典中。所以先閱讀文件,然後追加新的字典,然後保存。

全碼:

NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; 
[dict setValue:categoryField.text forKey:@"Category"]; 
[dict setValue:nameField.text forKey:@"Name"]; 
[dict setValue:eventField.text forKey:@"Event"]; 

[self writeDictionary:dict]; 

- (void)writeDictionary:(NSDictionary *)dict 
{ 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"yourPlist.plist"]; 

    NSMutableArray *savedStock = [[NSMutableArray alloc] initWithContentsOfFile: path]; 
    if(!savedStock) { 
     savedStock = [[[NSMutableArray alloc] initiWithCapacity:1]; 
    } 
    [savedStock addObject:dict]; 

    // see what';s there now 
    for (NSDictionary *dict in savedStock) { 
     NSLog(@"my Note : %@",dict); 
    } 
    // now save out 
    [savedStock writeToFile:path atomically:YES]; 
} 
+0

謝謝大衛。我知道這是我的問題背後的邏輯,但我在這裏有一個編碼問題,我認爲不是一個架構。 – LolaEnaMilo 2013-05-10 15:13:03

0

替換:

NSMutableArray *arr = [[NSMutableArray alloc] init]; 
[arr addObject:dict]; 
[arr writeToFile: path atomically:YES]; 

有了:

NSMutableArray *arr = [[NSMutableArray alloc] initWithContentsOfFile: path]; 
[arr addObject:dict]; 
[arr writeToFile: path atomically:YES]; 
+0

我做到了。現在它不打印任何東西。甚至不是最後一個。 – LolaEnaMilo 2013-05-10 15:10:26