0

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

分配信息.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];

}
4

1 回答 1

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,并在必要时将该数组保存到磁盘/从磁盘加载它。

于 2013-10-13T02:28:55.017 回答