gpt4 book ai didi

ios - 在Bundle的json文件中写字典,Swift 4,iOS

转载 作者:行者123 更新时间:2023-11-28 13:27:58 25 4
gpt4 key购买 nike

<分区>

我有一个本地 .json 文件,我想向其中读写数据。有了阅读,我没有问题:

let file = Bundle.main.url(forResource: "categories", withExtension: "json")
let data = try? Data(contentsOf: file)
let json = try? JSONSerialization.jsonObject(with: data, options: [])

但是写不行。我的代码(jsonData & file 不是 nil):

let dict = ["data": categories.compactMap { $0.dictionary }]
let file = Bundle.main.url(forResource: "categories", withExtension: "json")
let jsonData = try? JSONSerialization.data(withJSONObject: dict, options: [])
try? jsonData?.write(to: file!)

.json 文件里面看起来是这样的:

{
"data" : [
{
"id" : "new",
"name" : "New"
}]
}

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