[英]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.