ios NSData dataWithContentsOfFile 返回 null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20795756/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 22:54:02  来源:igfitidea点击:

NSData dataWithContentsOfFile returning null

iosiphoneobjective-cnsdata

提问by user3129278

I am trying to fetching a JSONfile which exist in my xcoderesources using this code

我正在尝试使用此代码获取资源JSON中存在的文件xcode

-(void)readJsonFiles
{
    NSString *str=[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"classes.json"];
    NSLog(@"Path: %@", str);
    NSData *fileData = [NSData dataWithContentsOfFile:str];
    NSLog(@"Data: %@", fileData);
    SBJsonParser *parser = [[SBJsonParser alloc] init] ;
    NSDictionary *jsonObject = [parser objectWithData:fileData];
    NSLog(@"%@", jsonObject);
}

path return me this link of file path

路径返回我这个文件路径链接

/Users/astutesol/Library/Application Support/iPhone Simulator/6.0/Applications/A4E1145C-500C-4570-AF31-9E614FDEADE4/The Gym Factory.app/classes.json

but when I log fileDatait return me null. I don't know where I am doing mistake.

但是当我登录fileData它时,它会返回给我null。我不知道我在哪里做错了。

回答by TheAmateurProgrammer

Instead of appending the path, use pathForResource:ofType:, so

而不是附加路径,使用pathForResource:ofType:,所以

NSString *str=[[NSBundle mainBundle] pathForResource:@"classes" ofType:@"json"];

NSData *fileData = [NSData dataWithContentsOfFile:str];

回答by JeremyP

It's failed to read your file for some reason. Use -dataWithContentsOfFile:options:error:to find out why.

由于某种原因无法读取您的文件。使用-dataWithContentsOfFile:options:error:找出原因。

NSError* error = nil;
NSData *fileData = [NSData dataWithContentsOfFile:str options: 0 error: &error];
if (fileData == nil)
{
   NSLog(@"Failed to read file, error %@", error);
}
else
{
    // parse the JSON etc
}

回答by TtheTank

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"MyFile" ofType:@"txt"];  
NSData *myData = [NSData dataWithContentsOfFile:filePath];  

回答by Kevin ABRIOUX

There is the solution for Swift 3 :

Swift 3 有解决方案:

//create a fil's path, decompose name and extension
let path = Bundle.main.path(forResource: "anim-gif-1", ofType: "gif")
//get the data asociated to this file
let file = NSData(contentsOfFile: path!)

回答by Manthan

You need to use pathforResource with resourcetype and check if there is data in your file.

您需要使用 pathforResource 和 resourcetype 并检查文件中是否有数据。

That should not be nil.

那不应该是零。

回答by Code Hunter

You can follow this to get NSData from your document directory

您可以按照此从您的文档目录中获取 NSData

NSString *stringPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"YOUR_FOLDER_NAME"];

stringPath  = [stringPath stringByAppendingPathComponent:@"JSON_FILE_NAME.json"];
NSLog(@"stringpath %@",stringPath);
NSData *fileData = [NSData dataWithContentsOfFile:stringPath];

Hope this will work for you.

希望这对你有用。