gpt4 book ai didi

ios - arrayWithContentsOfFile 和 arrayWithContentsOfURL 无

转载 作者:行者123 更新时间:2023-11-28 18:35:08 26 4
gpt4 key购买 nike

当我调用 [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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com