简体   繁体   中英

Issues writing to file in Swift 2.0

I am currently both reading and writing from a file in swift, and the reading works fine, while the writing seems to be doing nothing. Here is what I am using to read:

let path = NSBundle.mainBundle().pathForResource(filename, ofType: "txt")
let returnString = try? String(contentsOfFile: path!, encoding: NSUTF8StringEncoding)

and here is what I am trying to use to write:

let text = "hi mom"
let path = NSBundle.mainBundle().pathForResource(filename, ofType: "txt")
do{
    try text.writeToFile(path, atomically: false, encoding: NSUTF8StringEncoding);
}
catch{
    print("nope")
}

It is not throwing an error, but also not changing the file

You can't add/modify anything located inside the resources folder (bundled with your app). You can save it to the documents directory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM