0

我有以下代码来加载一些文件并将它们保存到磁盘:

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

    }

    });

}

但是,当我运行此代码时,会出现以下异常:

在此处输入图像描述

*终止应用程序由于未捕获的异常'NSInvalidArgumentException',原因是: ' - [__ NSArrayM getFileSystemRepresentation:最大长度:]:无法识别的选择发送到实例0xc8809e0' *第一掷调用堆栈:(0x3623012 0x3448e7e 0x36ae4bd 0x3612bbc 0x361294e 0x2e2d7b4 0x2e2d762 0x2e5bc85 0x2e83c7a 0x8d98 0x406053f 0x4072014 0x40632e8 0x4063450 0x95336e12 0x9531ecca) libc++abi.dylib:终止调用抛出异常 (lldb)

为什么会这样?

4

1 回答 1

2

似乎[dictionary objectForKey:@"paths"]在可疑行上包含一个NSMutableArray而不是一个NSString. 也许你的意思是[[dictionary objectForKey:@"paths"] objectAtIndex:0]相反。

于 2012-11-11T10:22:17.503 回答