如果目录中没有退出,如何创建CSV文件,如果退出,在Swift4.2中将数组数据添加到退出CSV中?

时间:2019-07-17 13:07:21

标签: csv nsfilemanager swift4.2 xcode10.1

如何将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")
    }

}

0 个答案:

没有答案