gpt4 book ai didi

ios - dictionaryWithContentsOfFile 返回 Nil 而不是 NSdictionary

转载 作者:行者123 更新时间:2023-11-29 10:40:32 25 4
gpt4 key购买 nike

我创建了一个名为 Dict.json 的文件。该文件的内容是有效的 json containing

{"mydata":[{ "A":4, "B":14, "C":7 }, { "A":4, "B":12, "C":7 }, { "A":34, "B":154, "C":6 }, { "A":34, "B":162, "C":6 }]}

我想从这个文件创建一个 NSDictonary。我尝试了以下但它返回 nil。

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Dict" ofType:@"json"]; NSMutableDictionary *newArr1=[NSMutableDictionary dictionaryWithContentsOfFile:filePath];

我也在检查文件不为零;

NSData *myData = [NSData dataWithContentsOfFile:filePath];
if (myData) {
NSLog("There is Data in File !!!!")
}

最佳答案

要加载json 数据,您需要NSJSONSerialization 从文件中获取json 数据

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Dict" ofType:@"json"]; 
NSData *jsonData = [NSData dataWithContentsOfFile:filePath];
NSMutableDictionary *dic1 = [[NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:nil] mutableCopy];

您的代码仅适用于 plist 文件,不适用于 json 文件。

关于ios - dictionaryWithContentsOfFile 返回 Nil 而不是 NSdictionary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24773520/

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