如何停止覆盖数据 [英] How to stop overwriting data
本文介绍了如何停止覆盖数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在我的iOS应用程式中储存一些资料。我使用下面的代码:
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString * documentsDirectory = [paths objectAtIndex:0];
NSString * path = [documentsDirectory stringByAppendingPathComponent:@yourPlist.plist];
//插入数据
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];
//检索数据
NSMutableArray * savedStock = [[NSMutableArray alloc] initWithContentsOfFile:path];
for(NSDictionary * dict in savedStock){
NSLog(@my Note:%@,dict);但是NSLog只显示最后一个数据...我想我i我会覆盖这里。我不能看到为什么,虽然!
我如何继续保存字典在数组没有覆盖?任何想法?
解决方案由于你是一个模型对象,它会更好,如果你包括save,remove,findAll,findByUniqueId的逻辑。将使得与模型对象的工作非常简单。
@interface注意:NSObject
@property(nonatomic,copy)NSString *
@property(nonatomic,copy)NSString * name;
@property(nonatomic,copy)NSString * event;
- (id)initWithDictionary:(NSDictionary *)dictionary;
/ *查找所有保存的注释* /
+(NSArray *)savedNotes;
/ *保存当前注释* /
- (void)save;
/ *从plist中删除注释* /
- (void)remove;
保存便签
Note * note = [Note new];
note.category = ...
note.name = ...
note.event = ...
[note save];
从已保存的列表中删除
//找到要删除的注释的引用
注意* note = self.savedNotes [index];
[note remove];
查找所有保存的记录
NSArray * savedNotes = [Note savedNotes];
I am trying to save in my iOS app some data. I use the following code :
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);
}
However the NSLog shows me only the last data... I suppose that i am overwriting here..I cant see why though!
How can i continue to save dictionaries in the array without overwriting? Any ideas?
解决方案 Since you are making a model object it would be better if you include save, remove, findAll, findByUniqueId kind of logic built into it. Will make working with the model object very simple.
@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;
Save a note
Note *note = [Note new];
note.category = ...
note.name = ...
note.event = ...
[note save];
Delete from saved list
//Find the reference to the note you want to delete
Note *note = self.savedNotes[index];
[note remove];
Find all saved notes
NSArray *savedNotes = [Note savedNotes];
这篇关于如何停止覆盖数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文