Search code examples
iosobjective-carchivenscoding

I need some help archiving an NSArray to hold some values and add/delete them later


I am doing an app as a starter to get some experience, I am making a simple app that stores assignments in a table view. So I have a table view as a main view and a plus button that pulls up a modal view controller that allows the user to enter information about the - class name, assignment title, assignment description, due date, and a switch to turn on/off notifications. These values are stored in a AssignmentInfo model. I need to be able to archive (NSCoding) these values, and add to them when new data is entered. Here is some sample code that might help give a better idea:

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

}

Solution

  • I'll link a post from the inimitable NSHipster on the subject. 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
    

    Once your class is setup you can save/restore from disk easily:

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

    So in your case you have a class like this:

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

    So you'd end up with an initWithCoder method like:

    #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;
    }
    

    and an encodeWithCoder like:

    - (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"];
    }
    

    Now you should be able to just add your newly created objects to an NSMutableArray, and when necessary save that array to disk/load it from disk.