我收到以下错误:
*** 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为null。文件路径是:
/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是否为空的情况下使其工作?
答案 0 :(得分: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];
}