【问题标题】:How to get data from a CSV in swift when using UIDocumentPickerViewController使用 UIDocumentPickerViewController 时如何快速从 CSV 获取数据
【发布时间】:2019-06-15 12:28:13
【问题描述】:

我找不到从 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 文件,然后将其导入到一个或多个数组中。但我无法找到一种方法来完成这项工作。

【问题讨论】:

    标签: ios swift uidocumentpickerviewcontroller


    【解决方案1】:

    我终于找到了解决问题的方法。代码如下:

    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()
                }
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-27
      • 2020-03-19
      • 1970-01-01
      相关资源
      最近更新 更多