gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 22:34:28 34 4
gpt4 key购买 nike

我正在开发一个应用程序,用户可以在其中创建一个包含 3 个字段的事件:

类别、名称、事件。在用户输入后,我有一个保存按钮,可以保存他的数据以供将来引用。然后,当他再次打开应用程序时,数据将显示在表格 View 中。

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

到目前为止我做了什么:

我用类别、名称、事件创建了一个“Note”类。

我的保存按钮的代码如下所示:

- (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!
*/
}

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

其次,如果我再次运行这段代码,我会覆盖数据,还是创建新条目?

我想看看如何每次都创建一个新条目。

另外,我想从我向他们展示的表中删除一个事件,所以我想看看删除是如何工作的。

我的“笔记”对象看起来像这样:

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

Source Code

关于ios - 如何在 iOS 应用程序中保存数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16480807/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com