Ios Swift.writeToFile不更新mainBundle中的JSON文件
我正在尝试一个简单的字典检索,更新键值并写回文件。出于某种原因,writeToFile不会更新主捆绑包中的文件 守则内容如下:Ios Swift.writeToFile不更新mainBundle中的JSON文件,ios,json,swift,Ios,Json,Swift,我正在尝试一个简单的字典检索,更新键值并写回文件。出于某种原因,writeToFile不会更新主捆绑包中的文件 守则内容如下: let filename = "testFile" if let path = NSBundle.mainBundle().pathForResource(filename, ofType: "json") { var error: NSError? let InputData: NSData? = NSData(contentsOfFile: path
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)
}
该文件如下所示:
{
"levelColumn" : 0,
"levelRow" : 0,
}
读取和更新工作正常。。。但是文件没有将levelRow更新为1
提前感谢。您无法写入主捆绑包。捆绑包中的所有文件都是只读的。在修改文件之前,将其复制到应用程序文档目录中
如果您需要捆绑包中的其他文件包含在应用程序中,您可以在开发过程中在documents目录中对其进行更新,然后在发布应用程序之前将其手动复制到捆绑包中。您无法在捆绑包中写入资源,请使用Cache或documents目录保存程序数据感谢快速响应。这是新的(正如你所看到的!)。