Ios 如何使用SwiftyJSON在swift中写入JSON?
我能够使用SwiftyJSON成功读取本地JSON文件。现在我想为待办事项列表编写一个字典数组,每个数组元素包括一个标题和一个时间戳。以下是我的JSON的外观:Ios 如何使用SwiftyJSON在swift中写入JSON?,ios,json,swift,swifty-json,Ios,Json,Swift,Swifty Json,我能够使用SwiftyJSON成功读取本地JSON文件。现在我想为待办事项列表编写一个字典数组,每个数组元素包括一个标题和一个时间戳。以下是我的JSON的外观: [ { "Content": "Walk the dog", "Created": "October 24, 2015", }, { "Content": "Hello", "Created": "October 25, 2015", } ] @IBAction func setJSON(_ sender: Any)
[
{
"Content": "Walk the dog",
"Created": "October 24, 2015",
},
{
"Content": "Hello",
"Created": "October 25, 2015",
}
]
@IBAction func setJSON(_ sender: Any) {
let file = Bundle.main.path(forResource: "Data", ofType: "json")
let data = try? Data(contentsOf: URL(fileURLWithPath: file!), options: NSData.ReadingOptions.dataReadingMapped)
if let data = data {
var json = JSON(data: data)
json.dictionaryObject = ["name":"Alex", "age":25]
loadJSON()
}
}
我有一个函数(不起作用),当我按下故事板上的按钮时,它应该会更改JSON数据。这看起来像:
[
{
"Content": "Walk the dog",
"Created": "October 24, 2015",
},
{
"Content": "Hello",
"Created": "October 25, 2015",
}
]
@IBAction func setJSON(_ sender: Any) {
let file = Bundle.main.path(forResource: "Data", ofType: "json")
let data = try? Data(contentsOf: URL(fileURLWithPath: file!), options: NSData.ReadingOptions.dataReadingMapped)
if let data = data {
var json = JSON(data: data)
json.dictionaryObject = ["name":"Alex", "age":25]
loadJSON()
}
}
注意,它没有返回任何错误,只是没有显示JSON中的任何差异
谢谢大家!
更新:
我尝试将JSON数据记录到控制台,但JSON数据似乎正在更改,我的问题只是将该数据保存到本地JSON文件。尝试将其记录到控制台?在我看来,它正在工作,您只是没有更新UI。@brandonscript感谢您的快速响应!我刚刚试过,JSON变量正在更改。我想我的问题是JSON没有保存到JSON文件中。您是否将其保存到JSON文件中?不你喜欢自己做这件事yourself@muescha我该怎么做呢?这正是我的问题…尝试将其记录到控制台?在我看来,它正在工作,您只是没有更新UI。@brandonscript感谢您的快速响应!我刚刚试过,JSON变量正在更改。我想我的问题是JSON没有保存到JSON文件中。您是否将其保存到JSON文件中?不你喜欢自己做这件事yourself@muescha我该怎么做呢?这正是我的问题。。。