【问题标题】:[__NSArrayM getFileSystemRepresentation:maxLength:]: unrecognized selector sent to instance[__NSArrayM getFileSystemRepresentation:maxLength:]: 无法识别的选择器发送到实例
【发布时间】:2012-11-11 10:02:18
【问题描述】:

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

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 0x2e83c7a 0x8d98 0x4053f 0x4072014 0x4063450 0x4063450 0x953362012 0x9533450 0x9533620 0x953362CCA) libc++abi.dylib:终止调用抛出异常 (lldb)

为什么会这样?

【问题讨论】:

  • 发生这种情况是因为 'dictionary' 为 nil 或 [dictionary objectForKey:@"paths"] 为 nil。分配后检查字典的值。还要检查 [NSData dataWithContentsOfURL:url] 是否不为零。
  • @NikosM 呃,一点也不。如果是nil,则会抛出另一个异常。
  • 说“下面的那一行是异常发生的地方”并没有多大用处,然后在那一行上做 100 件事!将行分解为每个单独的操作,看看你的错误发生在哪里。

标签: iphone objective-c ios xcode nsarray


【解决方案1】:

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

【讨论】:

  • 它应该包含一个 NSMutableArray
  • @PixelBitAppsLtd 很好,它仍然不是NSStringunarchiveObjectWithFile: 接受 NSString 参数,以防您没有阅读文档(您应该为此感到难过)。
  • 那有没有另一种方法可以“归档”和“取消归档”NSMutableArray?我必须将其发送到服务器,然后再次下载。它需要采用合适的格式才能放入 NSDictionary :-)
  • @PixelBitAppsLtd 如果您使用- [NSArray writeToFile:atomically:] 将数组写入文件,那么您可以简单地使用+ [NSArray arrayWithContentsOfFile:] 来检索它。
  • 但我需要将其添加到 NSDictionary,这就是为什么我认为最好将其存档:(
猜你喜欢
  • 2016-10-11
  • 1970-01-01
  • 1970-01-01
  • 2013-10-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多