0

I am trying to write a mutable array of custom objects, which has 5 data objects, to the disk. However, using the unarchiver always makes my array go null, and I cannot figure out why. Here is the method: (self.toDoItems is an NSMutableArray defined in this class)

- (void)viewDidLoad {
    [super viewDidLoad];
    self.toDoItems = [[NSMutableArray alloc] init];
    NSLog(@"toDOitems on load = %@",self.toDoItems);

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"deliveries.txt"];

    self.toDoItems = [NSKeyedUnarchiver unarchiveObjectWithFile:appFile];
    NSLog(@"toDOitems after unarchive = %@",self.toDoItems);

    [self loadInitialData1];

    //load array here
}

And here is the output:

2015-06-28 17:15:07.778 ToDoList[13493:645560] toDOitems on load = (
)
2015-06-28 17:15:07.780 ToDoList[13493:645560] toDOitems after unarchive =     (null)
2015-06-28 17:15:07.780 ToDoList[13493:645560] toDOitems = (null)
2015-06-28 17:18:42.498 ToDoList[13493:647529] 

And here is the implementation of NSCoding:

#import "ToDoItem.h"

@implementation ToDoItem


-(void)encodeWithCoder:(NSCoder *)encoder{
    [encoder encodeObject:self.total forKey:@"total"];
    [encoder encodeObject:self.tip forKey:@"tip"];
    [encoder encodeObject:self.percentage forKey:@"percentage"];
    [encoder encodeBool:self.completed forKey:@"completed"];
    [encoder encodeBool:self.isCreditCard forKey:@"isCreditCard"];

}

- (id)initWithCoder:(NSCoder *)decoder {
    if (self = [super init]) {
        self.total = [decoder decodeObjectForKey:@"total"];
        self.tip = [decoder decodeObjectForKey:@"tip"];
        self.percentage = [decoder decodeObjectForKey:@"percentage"];
        self.completed = [decoder decodeBoolForKey:@"completed"];
        self.isCreditCard = [decoder decodeBoolForKey:@"isCreditCard"];
    }
    return self;
}

@end
luk2302
  • 55,258
  • 23
  • 97
  • 137
Zach
  • 85
  • 1
  • 1
  • 9
  • What are the types of `total`, `tip`, `percentage`? Are the `NSNumber` Objects or primitive numeric types? – zaph Jun 28 '15 at 21:28
  • They are NSNumberObjects – Zach Jun 28 '15 at 21:30
  • `appFile` exists? NSLog it and check the file system to ensure it exists. Examine the contents with a HexEditor to see if it makes sense. All of the values at encode time are valid, non-nill? – zaph Jun 28 '15 at 21:36
  • Ok thank you I will try that. Also, can you tell me if this makes sense: what if there is no data when I first open the app? Will it set it to null, or will it just leave it empty? – Zach Jun 28 '15 at 21:37
  • Off hand I Don't know, try it. – zaph Jun 28 '15 at 21:43
  • As @zaph said, check the *saving* part. By the way, there is no sense in initializing self.toDoItems first before loading, as it gets overwritten anyway. And keep in mind that you might have to call mutableCopy to get a mutable version back. – Eiko Jun 29 '15 at 06:43

0 Answers0