我正在尝试一个简单的字典检索,更新键值并写回文件.由于某种原因,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?
提前致谢.
Mun*_*ndi 12
你不能写入主包.捆绑包中的所有文件都是只读的.在修改文件之前,将文件复制到应用程序文档目录中.
如果您需要捆绑包中的其他文件以包含在您的应用程序中,您可以在开发期间在文档目录中更新它,然后在发送应用程序之前手动将其复制到捆绑包中.
| 归档时间: |
|
| 查看次数: |
2795 次 |
| 最近记录: |