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

user3483203
  • 50,081
  • 9
  • 65
  • 94

1 Answers1

1

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

Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
  • 1
    If you need a reference to do so http://stackoverflow.com/questions/24097826/read-and-write-data-from-text-file/26557965#26557965 – Leo Dabus Apr 04 '16 at 21:41