1

我收到以下错误:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil'
*** First throw call stack:

导致错误的代码是:

NSDictionary *dict = nil;
    @synchronized(self) {
        dict = [pendingDictionarySaves objectForKey:file];
    }
    if (dict) return dict;

    @synchronized(self) {
        dict = [savedDictionaries objectForKey:file];
    }
    if (dict) return dict;

    NSData *plistData = [[NSFileManager defaultManager] contentsAtPath:file];

    NSError *error = nil;
    if ([[NSPropertyListSerialization class]
         respondsToSelector:@selector(propertyListWithData:options:format:error:)]) {
        return [NSPropertyListSerialization propertyListWithData:plistData
                                                         options:NSPropertyListImmutable
                                                          format:NULL
                                                           error:&error];
    }

基本上原因是因为 plistData 为空。文件路径是:

/Users/aditya15417/Library/Application Support/iPhone Simulator/5.1/Applications/A355D003-668C-4B81-80DC-66C968FE57D3/Library/Caches/NewsfeedCache/?type=news

这是我初始化路径的方式:

  NSString *path = [basePath stringByAppendingPathComponent:[streamURL uniqueFileName]];

    // Create the directories if needed
    if (![[NSFileManager defaultManager] fileExistsAtPath:path]) {
        [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:NO attributes:nil error:nil];
    }

    return path;

问题是如何在不必检查 pListData 是否为空的情况下让它工作?

4

1 回答 1

0

也许你最好使用 +[NSData dataWithContentsOfFile:options:error:]。如果读取失败,该方法至少会给您一个错误。就像是:

NSError *error = nil;
NSData *plistData = [NSData dataWithContentsOfFile:file options:0 error:&error];
if(!plistData) {
    NSLog(@"failed to read data: %@",error);
    return nil;
}

if ([[NSPropertyListSerialization class]
     respondsToSelector:@selector(propertyListWithData:options:format:error:)]) {
    return [NSPropertyListSerialization propertyListWithData:plistData
                                                     options:NSPropertyListImmutable
                                                      format:NULL
                                                       error:&error];
}
于 2013-02-25T17:24:52.247 回答