[英]reading a file using NSFileManager causing an error
我收到以下错误:
*** 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是否为null的情况下使其工作?
也许最好使用+ [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];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.