如何在iOS应用程序中保存数据? [英] How to save data , in an iOS application?
问题描述
我正在开发一个应用程序,用户在其中创建一个包含3个字段的事件:
I am developing an application where the user creates an event which has 3 fields:
类别,名称,事件。用户之后给出了他的条目,我有一个保存按钮,将保存他的数据以供将来参考。然后,当他再次打开应用程序时,数据将显示在表格视图中。
Category , name , event. After the user gives his entry , i have a save button that will save his data for future reference. Then when he opens the app again the data will be shown in a table View.
我如何在iOS上保存数据?我知道NSUserDefaults,但我很确定这不是这个例子的方式。
How exactly do i "save" data on iOS ? I know about the NSUserDefaults , but i am pretty sure this is not the way for this example.
到目前为止我做了什么:
我创建了一个包含Category,name,event的Note类。
I created a "Note" class with Category , name , event.
我的保存按钮的代码看起来像这样:
The code for my save button looks like this:
- (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!
*/
}
这是保存活动的正确方法吗?我现在如何检索我写的内容?
Is this the correct way to save an event? How can i retrieve now what i wrote ?
其次,如果我再次运行此代码,我会覆盖数据,还是创建新条目?
Secondly if i run this code again i ll overwrite data , or create new entry?
我想看看我每次都可以制作新的参赛作品。
I would like to see how i can make a new entry every time.
我也想从我正在呈现它们的表中删除一个事件,所以我想看看删除是如何工作的。
Also i would like to delete an event from the table that i am presenting them , so i would like to see how the delete would work.
我的Note对象看起来像这样:
My "Note" object looks like that:
@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文件
//Note.m file
- (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];
}
这篇关于如何在iOS应用程序中保存数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!