我目前正在快速读取和写入文件,读取工作正常,而写入似乎什么也没做。这是我用来阅读的内容:
let path = NSBundle.mainBundle().pathForResource(filename, ofType: "txt")
let returnString = try? String(contentsOfFile: path!, encoding: NSUTF8StringEncoding)
这是我试图用来写的内容:
let text = "hi mom"
let path = NSBundle.mainBundle().pathForResource(filename, ofType: "txt")
do{
try text.writeToFile(path, atomically: false, encoding: NSUTF8StringEncoding);
}
catch{
print("nope")
}
它没有抛出错误,但也没有更改文件
最佳答案
您无法添加/修改资源文件夹(与您的应用程序捆绑在一起)内的任何内容。您可以将其保存到文档目录中。
关于swift - 在 Swift 2.0 中写入文件时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36412904/