var file = ""
var text = ""
var path: URL?
override viewDidLoad(){
super.viewDidLoad()
file = "test2.csv" //this is the file I will write to
if let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first {
path = dir.appendingPathComponent(file)
do {
text = "Hello"
try text.write(to: path!, atomically: true, encoding: String.Encoding.utf8)
}
catch {/* error handling here */}
}
getInsightResult()
}
This piece of code is writing "Hello" to "test2.csv" perfectly in my viewDidLoad() method. However, when I run that excerpt of code in a separate method called getInsightResult(), which I call in viewDidLoad, the text that is being written is blank. However, when I print out the text it is not empty, but displays the correct text.
func getInsightResult() {
for x in 0...4{
for y in 0...4{
if(y != 3){
do{
let temp = arrayOfDataArrays[y]
text = String(temp[x])
print("Tester:\(temp[x])")
print("Text:" + text)
try text.write(to: path!, atomically: true, encoding: String.Encoding.utf8)
}
catch {/* error handling here */}
}
}
text = "\n"
do{try text.write(to: path!, atomically: true, encoding: String.Encoding.utf8)}
catch {/* error handling here */}
}
}