0
votes

I have the following code to load some files and save them to disk:

NSDictionary *dictionary = [NSKeyedUnarchiver unarchiveObjectWithData:[NSData dataWithContentsOfURL:url]];
// THE LINE BELOW IS WHERE THE EXCEPTION OCCURS
NSMutableArray *paths = [[NSMutableArray alloc] initWithArray:[NSKeyedUnarchiver unarchiveObjectWithFile:[dictionary objectForKey:@"paths"]]];

if(dictionary)
{

    dispatch_async(dispatch_get_main_queue(), ^{

    NSLog(@"DOWNLOADED PATHS: %@", paths);

    NSFileManager *filemgr;
    NSString *docsDir;
    NSArray *dirPaths;

    filemgr = [NSFileManager defaultManager];

    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    docsDir = [dirPaths objectAtIndex:0];

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    for(NSObject *obj in paths)
    {

        NSString *identification = [defaults objectForKey:@"LatestID"];

        NSString *pageno = [NSString stringWithFormat:@"%i", [paths indexOfObject:obj]];
        NSString *name = [NSString stringWithFormat:@"%@paths%@.archive", identification, pageno];

        NSString *dataFilePath = [[NSString alloc] initWithString: [docsDir
                                                                    stringByAppendingPathComponent:name]];

        [NSKeyedArchiver archiveRootObject:paths toFile:dataFilePath];

    }

    });

}

However, when I run this code, the following exception appears:

enter image description here

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM getFileSystemRepresentation:maxLength:]: unrecognized selector sent to instance 0xc8809e0' * First throw call stack: (0x3623012 0x3448e7e 0x36ae4bd 0x3612bbc 0x361294e 0x2e2d7b4 0x2e2d762 0x2e5bc85 0x2e83c7a 0x8d98 0x406053f 0x4072014 0x40632e8 0x4063450 0x95336e12 0x9531ecca) libc++abi.dylib: terminate called throwing an exception (lldb)

Why is this happening?

1
This is happening because either 'dictionary' is nil or [dictionary objectForKey:@"paths"] is nil. Check the value of dictionary after it is assigned. Also check that [NSData dataWithContentsOfURL:url] is not nil. - NikosM
@NikosM Eh, not at all. If it was nil, another exception would be thrown. - user529758
It's not much use to say 'THE LINE BELOW IS WHERE THE EXCEPTION OCCURS' an then do 100 things on that line! Break the line down into each individual operation and see where your error occurs. - deanWombourne

1 Answers

2
votes

It seems that [dictionary objectForKey:@"paths"] on the suspicious line contains an NSMutableArray instead of an NSString. Perhaps you meant [[dictionary objectForKey:@"paths"] objectAtIndex:0] instead.