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!