0

I'm making simple program that can archive my object and unarchive it when launching.

I have a lists.m where mutable array called favouritesList get store into file using archive. Inside list, i have simple object called favouriteItem

So, the problem happens with this code

-(void) loadList
{

self.favouritesList =
[[NSKeyedUnarchiver unarchiveObjectWithFile:self.fileName] mutableCopy];
if(self.favouritesList == nil)
    self.favouritesList = [[NSMutableArray alloc]init]; 

}

The exception that i've got is this [NSKeyedUnarchiver initForReadingWithData:]: incomprehensible archive (0x62, 0x70, 0x6c, 0x69, 0x73, 0x74, 0x30, 0x30)'

i know i'm unarchiving with file that is empty (actually it gets copied from the file from bundle which is also empty).

So, how can i avoid this when i still want my app to load data from file when launching. When i used as following code

-(void) loadData {
    self.URLLists = [NSMutableArray arrayWithContentsOfFile:self.path];
    if(self.URLLists == nil) {
     self.URLLists = [[NSMutableArray alloc]init];
    }

}

Here, even though the file at self.path was empty, it return nil and i can just say if it returned nil, i will assign new one for the first time. But, with NSkeyedUnarchiver it works a bit differently.. could you explain how it is different and how can i avoid this error..? Thank you.

Rashad
  • 11,057
  • 4
  • 45
  • 73
denis_choe
  • 667
  • 6
  • 15

2 Answers2

2

The easier way out is to use exception handling:

@try {
            self.favouritesList =
[[NSKeyedUnarchiver unarchiveObjectWithFile:self.fileName] mutableCopy];
     }
     @catch ( NSException *e ) {
            self.favouritesList = [[NSMutableArray alloc]init]; // add more code as needed
     }

Apple's documentation is here.

https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Exceptions/Tasks/HandlingExceptions.html#//apple_ref/doc/uid/20000059-BBCHGJIJ

Totoro
  • 3,398
  • 1
  • 24
  • 39
  • how about the case of arrayWithContentsOfFile ?? I don't need to handle any exception cuz it doesn't throw any? – denis_choe Apr 24 '14 at 03:24
  • 1
    @denis_choe: Yes. If you read the docs for [unarchiveObjectWithFile:](https://developer.apple.com/library/ios/documentation/cocoa/reference/foundation/Classes/NSKeyedUnarchiver_Class/Reference/Reference.html#//apple_ref/occ/clm/NSKeyedUnarchiver/unarchiveObjectWithFile:) and [arrayWithContentsOfFile:](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSArray_Class/NSArray.html#//apple_ref/occ/clm/NSArray/arrayWithContentsOfFile:), they each say how they react to an empty file. NSKeyedUnarchiver throws an exception and NSArray just returns nil. – Chuck Apr 24 '14 at 03:31
0

Rather than try to unarchive an empty file, why don't you just have the file contain an empty array? Then you don't need any special cases.

Chuck
  • 234,037
  • 30
  • 302
  • 389
  • you mean make that file with empty array in.. where..? i mean inside viewdidload? – denis_choe Apr 24 '14 at 03:38
  • 1
    @denis_choe: I mean the file that you're trying to unarchive here, the one that you copy from your bundle that's empty. Instead of storing an empty file in your bundle and copying it, why not have the file contain an empty array? – Chuck Apr 24 '14 at 04:19