如何在iOS应用程序中保存数据?

我正在开发一个应用程序,用户创build一个有3个字段的事件:

类别,名称,事件。 用户input后,我有一个保存button,将保存他的数据供将来参考。 然后,当他再次打开应用程序时,数据将显示在表格视图中。

我如何在iOS上“保存”数据? 我知道NSUserDefaults,但我很确定这不是这个例子的方式。

到目前为止,我做了什么:

我创build了一个类别,名称,事件的“注意”类。

我保存button的代码如下所示:

- (IBAction)save:(id)sender { //creating a new "note" object Note *newNote = [[Note alloc]init]; newNote.category = categoryField.text; newNote.name = nameField.text; newNote.event = eventField.text; // do whatever you do to fill the object with data NSData* data = [NSKeyedArchiver archivedDataWithRootObject:newNote]; /* Now we create the path to the documents directory for your app */ NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; /* Here we append a unique filename for this object, in this case, 'Note' */ NSString* filePath = [documentsDirectory stringByAppendingString:@"Note"]; /* Finally, let's write the data to our file */ [data writeToFile:filePath atomically:YES]; /* We're done! */ } 

这是保存事件的正确方法吗? 我现在如何检索我写的东西?

其次,如果我再次运行这个代码,我会覆盖数据,或创build新的条目?

我想看看我可以每次做一个新的入门。

另外我想从表中删除一个事件,我介绍他们,所以我想看看如何删除工作。

我的“注意”对象看起来像这样:

 @interface Note : NSObject <NSCoding> { NSString *category; NSString *name; NSString *event; } @property (nonatomic, copy) NSString *category; @property (nonatomic, copy) NSString *name; @property (nonatomic, copy) NSString *event; @end 

尝试

 //Note.h #define kNoteCategory @"Category" #define kNoteName @"Name" #define kNoteEvent @"Event" @interface Note : NSObject @property (nonatomic, copy) NSString *category; @property (nonatomic, copy) NSString *name; @property (nonatomic, copy) NSString *event; - (id)initWithDictionary:(NSDictionary *)dictionary; + (NSArray *)savedNotes; - (void)save; 

//Note.m文件

 - (id)initWithDictionary:(NSDictionary *)dictionary { self = [super init]; if (self) { self.category = dictionary[kNoteCategory]; self.name = dictionary[kNoteName]; self.event = dictionary[kNoteEvent]; } return self; } + (NSString *)userNotesDocumentPath { NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:@"UserNotes.plist"]; return documentsPath; } + (NSArray *)savedNotes { NSString *documentsPath = [self userNotesDocumentPath]; NSArray *savedNotes = [NSArray arrayWithContentsOfFile:documentsPath]; NSMutableArray *savedUserNotes = [@[] mutableCopy]; for (NSDictionary *dict in savedNotes) { Note *note = [[Note alloc]initWithDictionary:dict]; [savedUserNotes addObject:note]; } return savedUserNotes; } - (NSDictionary *)userNoteDictionary { NSMutableDictionary *dict = [NSMutableDictionary dictionary]; if (self.category) { dict[kNoteCategory] = self.category; } if (self.name) { dict[kNoteName] = self.name; } if (self.event) { dict[kNoteEvent] = self.event; } return dict; } - (void)saveUserNotesToPlist:(NSArray *)userNotes { NSMutableArray *mutableUserNotes = [@[] mutableCopy]; for (Note *note in userNotes) { NSDictionary *dict = [note userNoteDictionary]; [mutableUserNotes addObject:dict]; } NSString *documentsPath = [Note userNotesDocumentPath]; [mutableUserNotes writeToFile:documentsPath atomically:YES]; } #pragma mark - Save - (void)save { NSMutableArray *savedNotes = [[Note savedNotes] mutableCopy]; [savedNotes addObject:self]; [self saveUserNotesToPlist:savedNotes]; } 

保存一个笔记

 - (IBAction)save:(id)sender { //creating a new "note" object Note *newNote = [[Note alloc]init]; newNote.category = categoryField.text; newNote.name = nameField.text; newNote.event = eventField.text; //Saves the note to plist [newNote save]; //To get all saved notes NSArray *savedNotes = [Note savedNotes]; } 

源代码

您可以使用NSKeyedUnArchiver来检索数据。 如果您尝试在相同的文件path中写入,则会覆盖写入

您可以使用核心数据保存所有数据,并在需要时将其删除。 上面的代码总是创buildNote类的新对象,所以每次你有新的数据,但是当你尝试用相同的名字“Note”写入时。 它总是覆盖旧的数据。

除此之外,所有的都是正确的,你可以使用Sqlite在本地保存你的应用程序的数据。

这只是一个文件,但接受所有标准的SQL语句。

这也是在本地保存数据的一种方法。

为了通过NSUserDefaults保存数据,我使用了GVUserDefaults

用法

在GVUserDefaults上创build一个类别,在.h文件中添加一些属性,并在.m文件中创build@dynamic。

 // .h @interface GVUserDefaults (Properties) @property (nonatomic, weak) NSString *userName; @property (nonatomic, weak) NSNumber *userId; @property (nonatomic) NSInteger integerValue; @property (nonatomic) BOOL boolValue; @property (nonatomic) float floatValue; @end 

 // .m @implementation GVUserDefaults (Properties) @dynamic userName; @dynamic userId; @dynamic integerValue; @dynamic boolValue; @dynamic floatValue; @end 

现在,您可以简单地使用[GVUserDefaults standardUserDefaults].userName来代替使用[[NSUserDefaults standardUserDefaults] objectForKey:@"userName"]

您甚至可以通过设置属性保存默认值:

 [GVUserDefaults standardUserDefaults].userName = @"myusername"; 

看,我给你通用的想法,你可以根据你的要求使用这个代码。

1)获取yourPlist.plist文件的“path”:

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

2)将数据插入到yourPlist中:

 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]; 

3)从yourPlist中检索数据:

 NSMutableArray *savedStock = [[NSMutableArray alloc] initWithContentsOfFile: path]; for (NSDictionary *dict in savedStock) { NSLog(@"my Note : %@",dict); }