如何将Array数据添加到CSV文件中?如果已经将CSV文件名退出,则将其添加到CSV文件中;如果尚未退出,请创建新的CSV文件,然后将Array数据附加到新的CSV文件中。
我在哪里做错了?
func createCSVWrite(from recArray:[Dictionary<String, AnyObject>]) {
let fileManager = FileManager.default
do {
let path = try fileManager.url(for: .documentDirectory, in: .allDomainsMask, appropriateFor: nil, create: false)
let fileURL = path.appendingPathComponent("\
(lblBabyName.text!).csv")
print(fileURL)
let pathsss = fileURL.path
let stream = OutputStream(toFileAtPath: pathsss, append: false)!
let csv = try! CSVWriter(stream: stream)
if fileManager.fileExists(atPath: pathsss) {
print("FILE AVAILABLE")
for dct in recArray {
try! csv.write(row: ["\(String(describing: dct["timess"]!))", "\(String(describing: dct["tempss"]!))"])
}
} else {
print("FILE NOT AVAILABLE")
try! csv.write(row: ["Date & Time", "Temperature"])
for dct in recArray {
try! csv.write(row: ["\(String(describing: dct["timess"]!))", "\(String(describing: dct["tempss"]!))"])
}
}
} catch {
print("error creating file")
}
}