小编swi*_*963的帖子

Swift .writeToFile不会更新mainBundle中的JSON文件

我正在尝试一个简单的字典检索,更新键值并写回文件.由于某种原因,writeToFile不会更新主包中的文件.

代码如下:

let filename = "testFile"
if let path = NSBundle.mainBundle().pathForResource(filename, ofType: "json") {
    var error: NSError?
    let InputData: NSData? = NSData(contentsOfFile: path, options: NSDataReadingOptions(), error: &error)
    var jsonDictionary: NSMutableDictionary = NSJSONSerialization.JSONObjectWithData(InputData!, options: NSJSONReadingOptions.MutableContainers, error: &error) as NSMutableDictionary

    jsonDictionary.setValue(1, forKey: "levelRow")

    let options = NSJSONWritingOptions.PrettyPrinted
    var outputData : NSData? =  NSJSONSerialization.dataWithJSONObject(jsonDictionary, options: options, error: &error)

    outputData?.writeToFile(path, atomically: true)
}
Run Code Online (Sandbox Code Playgroud)

该文件如下所示:

{
    "levelColumn" : 0,
    "levelRow" : 0,
}
Run Code Online (Sandbox Code Playgroud)

读取和更新工作正常...但文件不会更新levelRow为1?

提前致谢.

json ios swift

2
推荐指数
1
解决办法
2795
查看次数

标签 统计

ios ×1

json ×1

swift ×1