gpt4 book ai didi

ios - 我需要一些帮助来归档 NSArray 以保存一些值并稍后添加/删除它们

转载 作者:行者123 更新时间:2023-11-29 03:34:46 25 4
gpt4 key购买 nike

我正在制作一个应用程序作为初学者以获得一些经验,我正在制作一个简单的应用程序,将作业存储在 TableView 中。因此,我有一个 TableView 作为主视图和一个加号按钮,该按钮拉出一个模态视图 Controller ,允许用户输入有关以下内容的信息:类(class)名称、作业标题、作业描述、截止日期和打开的开关/关闭通知。这些值存储在AssignmentInfo 模型中。我需要能够存档(NSCoding)这些值,并在输入新数据时添加到它们。以下是一些示例代码,可能有助于提供更好的想法:

AssignmentInfo.h -

@property (nonatomic,strong)NSString *className;
@property (nonatomic,strong)NSString *assignmentDescription;
@property (nonatomic,strong)NSString *assignmentTitle;
@property (nonatomic,strong)NSString *dateTimeString;
@property (nonatomic)bool notifcationStatus;

AddEditViewController.m -

{
IBOutlet UIDatePicker *dateTimePicker;
}

@property (nonatomic, strong) IBOutlet UITextField *className;
@property (nonatomic, strong) IBOutlet UITextField *assignmentTitle;
@property (nonatomic, strong) IBOutlet UITextField *assignmentDescription;
@property (nonatomic, strong) IBOutlet UISwitch *procrastinationNotificationSwitch;
@property (nonatomic,strong)AssignmentInfo *assignmentInfo;

AddEditViewController.m -

- (IBAction)addTheInfo:(id)sender {

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
dateFormatter.timeZone = [NSTimeZone defaultTimeZone];
dateFormatter.timeStyle = NSDateFormatterShortStyle;
dateFormatter.dateStyle = NSDateFormatterShortStyle;
NSString *dateTimeString = [dateFormatter stringFromDate: dateTimePicker.date];
self.assignmentInfo.className = self.className.text;
self.assignmentInfo.assignmentTitle = self.assignmentTitle.text;
self.assignmentInfo.assignmentDescription = self.assignmentDescription.text;
self.assignmentInfo.dateTimeString = dateTimeString;
NSLog(@"%@",self.assignmentInfo.className);

[self dismissViewControllerAnimated:YES completion:nil];

}

最佳答案

我将链接来自无与伦比的 NSHipster 的关于该主题的帖子。 http://nshipster.com/nscoding/

NSCoding is a simple protocol, with two methods: -initWithCoder: and encodeWithCoder:. Classes that conform to NSCoding can be serialized and deserialized into data that can be either be archived to disk or distributed across a network.

@interface Book : NSObject <NSCoding>
@property NSString *title;
@property NSString *author;
@property NSUInteger pageCount;
@property NSSet *categories;
@property (getter = isAvailable) BOOL available;
@end

@implementation Book

#pragma mark - NSCoding

- (id)initWithCoder:(NSCoder *)decoder {
self = [super init];
if (!self) {
return nil;
}

self.title = [decoder decodeObjectForKey:@"title"];
self.author = [decoder decodeObjectForKey:@"author"];
self.pageCount = [decoder decodeIntegerForKey:@"pageCount"];
self.categories = [decoder decodeObjectForKey:@"categories"];
self.available = [decoder decodeBoolForKey:@"available"];

return self;
}

- (void)encodeWithCoder:(NSCoder *)encoder {
[encoder encodeObject:self.title forKey:@"title"];
[encoder encodeObject:self.author forKey:@"author"];
[encoder encodeInteger:self.pageCount forKey:@"pageCount"];
[encoder encodeObject:self.categories forKey:@"categories"];
[encoder encodeBool:[self isAvailable] forKey:@"available"];
}

@end

设置好类(class)后,您可以轻松地从磁盘保存/恢复:

// Archive
[NSKeyedArchiver archiveRootObject:books toFile:@"/path/to/archive"];

// Unarchive
[NSKeyedUnarchiver unarchiveObjectWithFile:@"/path/to/archive"];

所以在你的情况下,你有一个这样的类(class):

@property (nonatomic, strong) NSString *className;
@property (nonatomic, strong) NSString *assignmentDescription;
@property (nonatomic, strong) NSString *assignmentTitle;
@property (nonatomic, strong) NSString *dateTimeString;
@property (nonatomic, assign) BOOL notifcationStatus;

所以你最终会得到一个 initWithCoder 方法,例如:

#pragma mark - NSCoding

- (id)initWithCoder:(NSCoder *)decoder {
self = [super init];
if (!self) {
return nil;
}

self.className = [decoder decodeObjectForKey:@"className"];
self.assignmentDescription = [decoder decodeObjectForKey:@"assignmentDescription"];
self.assignmentTitle = [decoder decodeIntegerForKey:@"assignmentTitle"];
self.dateTimeString = [decoder decodeObjectForKey:@"dateTimeString"];
self.notifcationStatus = [decoder decodeBoolForKey:@"notifcationStatus"];

return self;
}

和一个encodeWithCoder,例如:

- (void)encodeWithCoder:(NSCoder *)encoder {
[encoder encodeObject:self.className forKey:@"className"];
[encoder encodeObject:self.assignmentDescription forKey:@"assignmentDescription"];
[encoder encodeInteger:self.assignmentTitle forKey:@"assignmentTitle"];
[encoder encodeObject:self.dateTimeString forKey:@"dateTimeString"];
[encoder encodeBool:self.notifcationStatus forKey:@"notifcationStatus"];
}

现在您应该能够将新创建的对象添加到 NSMutableArray,并在必要时将该数组保存到磁盘/从磁盘加载它。

关于ios - 我需要一些帮助来归档 NSArray 以保存一些值并稍后添加/删除它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19340922/

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