0

I got a problem when I was trying to write data into a csv file. The app reads data from a csv file, and during the application running, the data will change, and when it changes, it will call a function to write the data back to the csv file, but it doesn't work. thanks a lot!!

The code is listed below

var content = ""
        if content != ""{
            print("have content")
        }
        for tag in tags{
            let line = "\(tag.birdIndex),\(tag.isSeen)\n"
            content = content + line
        }
        if let path = Bundle.main.path(forResource: "birdLogVersion1", ofType: "csv"){
            do {
                let data = try String(contentsOfFile: path, encoding: String.Encoding.utf8)
                print(data)
            }catch{

            }
            //let url = NSURL(fileURLWithPath: path)
            do {
                try content.write(toFile: path, atomically: true, encoding: String.Encoding.utf8)
                //(to: url as URL, atomically: true, encoding: String.Encoding.utf8)
                print("tried write")
            }catch{
                //error
            }
        }else{

        }

when I call this function again, print(data) shows that data has already been written into this csv file, but when I close the app, and restart it again, the csv file still be the same, didn't change at all.

Do I have to do something like save file after write data into a csv file? Is it right to write info into the csv file in app? Thanks again!

Jason
  • 9
  • 3
  • You can't write in (NS)Bundle. – Larme Apr 28 '17 at 15:58
  • Thanks a lot for your help! but I used the FileManager to check this path, it shows that this file is writable. I can write info to it, but it will not save. if let path = Bundle.main.path(forResource: "birdLogVersion1", ofType: "csv"), if fileManager.isWritableFile(atPath: path). – Jason Apr 28 '17 at 23:54
  • I tried to not write in Bundle, it was working. Thanks a lot! – Jason Apr 29 '17 at 10:02

0 Answers0