作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我调用 [NSArray arrayWithContentsOfFile:] 时,我得到了 nil。
NSURL *fileURL = [NSURL URLWithString:className relativeToURL:[self JSONDataRecordsDirectory]];
return [NSArray arrayWithContentsOfFile:[NSString stringWithFormat:@"%@", [fileURL path]]];
或者,我在调用时也得到 nil:
[NSArray arrayWithContentsOfURL:]
我所有文件的结果都在 JSON 中: https://gist.github.com/neverbendeasy/03d047a6789b0ae14e1f
当我检查 fileURLs 时,数据就在那里。
我错过了什么?
最佳答案
您不能使用 NSArray arrayWith...
加载 JSON 文件。该方法只能加载以数组为根的plist文件。
如果您的文件是 JSON 文件,您应该使用:
NSData *jsonData = [NSData dataWithContentsOfURL:fileURL];
NSError *error = nil;
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
if (jsonArray) {
// do something with jsonArray
} else {
NSLog(@"Couldn't load JSON from %@: %@", fileURL, error);
}
这假设 JSON 文件的顶层是一个数组。
关于ios - arrayWithContentsOfFile 和 arrayWithContentsOfURL 无,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20556166/
这有点基础;我正在尝试检索 iPhone 应用程序的 http 数据。 我有 www.myhost.com/test.html 上面写着 testtest2 然后我有 NSURL *baseURL =
当我调用 [NSArray arrayWithContentsOfFile:] 时,我得到了 nil。 NSURL *fileURL = [NSURL URLWithString:className
我太笨了,不知道这个问题的答案 - 有人确切知道答案吗? NSMutableArray *happyDictionaryEntries; @property (nonatomic, retain) N
我是一名优秀的程序员,十分优秀!