NSData dataWithContentsOfFile返回null [英] NSData dataWithContentsOfFile returning null

查看:823
本文介绍了NSData dataWithContentsOfFile返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to fetching a JSON file which exist in my xcode resources using this code

-(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

但是当我记录 fileData 它给我 null 。我不知道我在哪里做错了。

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

推荐答案

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

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

NSData *fileData = [NSData dataWithContentsOfFile:str];

这篇关于NSData dataWithContentsOfFile返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆