我正在做一个应用程序作为初学者获得一些经验,我正在制作一个简单的应用程序,在表视图中存储分配。所以我有一个表视图作为主视图和一个加号按钮,它提取一个模态视图控制器,允许用户输入有关 - 类名称,作业标题,作业说明,截止日期和开关打开/的信息关闭通知。这些值存储在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];
}
答案 0 :(得分:1)
我将链接来自无与伦比的NSHipster的帖子。 http://nshipster.com/nscoding/
NSCoding是一个简单的协议,有两种方法:-initWithCoder:和 encodeWithCoder:。符合NSCoding的类可以序列化 并反序列化为可以存档到磁盘或数据的数据 分布在网络上。
@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
设置好类后,您可以轻松地从磁盘保存/恢复:
// Archive
[NSKeyedArchiver archiveRootObject:books toFile:@"/path/to/archive"];
// Unarchive
[NSKeyedUnarchiver unarchiveObjectWithFile:@"/path/to/archive"];
所以在你的情况下你有一个这样的课:
@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中,并在必要时将该阵列保存到磁盘/从磁盘加载。