我有以下代码来加载一些文件并将它们保存到磁盘:
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:maxLength:]:无法识别的选择器发送到实例0xc8809e0' * 第一次抛出调用堆栈: (0x3623012 0x3448e7e 0x36ae4bd 0x3612bbc 0x361294e 0x2e2d7b4 0x2e2d762 0x2e5bc85 0x2e83c7a 0x8d98 0x406053f 0x4072014 0x40632e8 0x4063450 0x95336e12 0x9531ecca) libc ++ abi.dylib:terminate调用抛出异常 (LLDB)
为什么会这样?
答案 0 :(得分:2)
似乎可疑行上的[dictionary objectForKey:@"paths"]
包含NSMutableArray
而不是NSString
。也许你的意思是[[dictionary objectForKey:@"paths"] objectAtIndex:0]
。