使用 UIDocumentPickerViewController 时如何从 swift 中的 CSV 中获取数据

How to get data from a CSV in swift when using UIDocumentPickerViewController

我找不到从 CSV 文件导入数据的方法。我希望将数据加载到数组中。

我已经尝试用谷歌搜索这个,但我发现似乎没有任何东西适合我的情况

这是我的 UIDocumentPickerViewController。

func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
    let urlst = "\(urls)"

    if urlst.fileExtension() == "csv]" || urlst.fileExtension() == "csv" {
        // Start Import Action From CSV File
    } else {
        let alert = UIAlertController(title: "An Error Occured!", message: "The file you were trying to inport is not supported.  Only csv is support.", preferredStyle: .alert)

        alert.addAction(UIAlertAction(title: "Ok", style: .default , handler: nil))

        alert.addAction(UIAlertAction(title: "Try Again", style: .default , handler: { (UIAlertAction) in
            self.toImport()
        }))

        self.present(alert, animated: true, completion: nil)
    }
}

我的预期结果是获取用户使用 UIDocumentPickerViewController 选择的 csv 文件,然后将其导入一个或多个数组。但是我找不到方法来完成这项工作。

我终于找到了解决问题的办法。这是代码:

let path = url.path
        let importer = CSVImporter<exportDataImport>(path: path)
        importer.startImportingRecords { recordValues -> exportDataImport in
            return exportDataImport(name: recordValues[0], pricePer: recordValues[2], amount: recordValues[1], isComplte: recordValues[4], Qty: recordValues[3])
            }.onFinish { importedRecords in

            var tempArray = [exportDataImport]()
            for record in importedRecords {
                tempArray.append(record)
            }

            if tempArray.count > 0 {
                tempArray.removeFirst(1)
                self.importArray = tempArray
                self.startImport()
            }
        }