【发布时间】:2018-11-17 12:57:51
【问题描述】:
我正在使用 CSVImporter pod 来解析从 iCloud Drive 上传的 CSV 文件,每行解析的目的都是要上传到我的数据库。
当importFile() 被执行时,它会打印路径,但随后会打印"The CSV file couldn't be read."
Q1:我做错了什么?
import UIKit
import MobileCoreServices
import CSVImporter
class importBatchVC: UIViewController,UIDocumentPickerDelegate,UINavigationControllerDelegate {
var path=""
var docURL = URL(string: "")
@IBAction func chooseDoc(_ sender: Any) {
let importMenu = UIDocumentPickerViewController(documentTypes: [String(kUTTypeContent),String(kUTTypePlainText)], in: .import)
importMenu.delegate = self
importMenu.modalPresentationStyle = .formSheet
self.present(importMenu, animated: true, completion: nil
}
@IBAction func importFile(_ sender: Any) {
if docURL==nil {
let alert = UIAlertController(title: "Error", message: "Please select a spreadsheet.", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
} else {
do {
self.path = String(describing:docURL)
print(path)
let importer = CSVImporter<[String]>(path: path)
importer.startImportingRecords { $0 }.onFail {
print("The CSV file couldn't be read.")
}.onProgress { importedDataLinesCount in
print("\(importedDataLinesCount) lines were already imported.")
}.onFinish { importedRecords in
print("Did finish import with \(importedRecords.count) records.")
}
}
}
}
@IBAction func cancel(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
}
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
print("The Url is : \(String(describing: url))")
let documentDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
do {
try FileManager.default.moveItem(at: url.standardizedFileURL, to: documentDirectory.appendingPathComponent(url.lastPathComponent))
self.docURL = documentDirectory.appendingPathComponent(url.lastPathComponent)
print("now check this: \(docURL!)")
} catch {
print(error)
}
}
func documentMenu(_ documentMenu: UIDocumentPickerViewController, didPickDocumentPicker documentPicker: UIDocumentPickerViewController) {
documentPicker.delegate = self
present(documentPicker, animated: true, completion: nil)
}
}
注意:这是作为“重复”关闭的转发问题,上面的代码是建议的实现。
我最初和首选的实现是通过documentPicker 获取 iCloud 驱动器文件的链接并即时解析/上传到数据库,但我遇到了与现在相同的错误:“CSV 文件无法”不能读。”
Leo Dabus 提供的解释是:
"UIDocumentPickerModeImport The URL refers to a copy of the selected document. This document is a temporary file. It remains available only until your application terminates. To keep a permanent copy, you must move this file to a permanent location inside your sandbox."
Q2: 考虑到我不需要保留一个永久文件-只需要保留它直到它被解析然后它就会在我的数据库中,为什么我需要将它导入到documentDirectory,有没有我可以通过从 documentPicker 获得的链接来解析它吗?
我的初始实现代码:
import UIKit
import MobileCoreServices
import CSVImporter
class importBatchVC: UIViewController,UIDocumentPickerDelegate,UINavigationControllerDelegate {
var path=""
var docURL = URL(string: "")
@IBAction func chooseDoc(_ sender: Any) {
let importMenu = UIDocumentPickerViewController(documentTypes: [String(kUTTypeContent),String(kUTTypePlainText)], in: .import)
importMenu.delegate = self
importMenu.modalPresentationStyle = .formSheet
self.present(importMenu, animated: true, completion: nil)
}
@IBAction func importFile(_ sender: Any) {
if docURL==nil {
let alert = UIAlertController(title: "Error", message: "Please select a spreadsheet.", preferredStyle: UIAlertControllerStyle.alert)
alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
}
else{
do {
self.path = docURL!.path
print(path)
let importer = CSVImporter<[String]>(path: path)
importer.startImportingRecords { $0 }.onFinish { importedRecords in
for record in importedRecords {
// record is of type [String] and contains all data in a line
print(record)
}
}
}
}
}
@IBAction func cancel(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
docURL = url as URL
print("The Url is : \(String(describing: url))")
}
func documentMenu(_ documentMenu: UIDocumentPickerViewController, didPickDocumentPicker documentPicker: UIDocumentPickerViewController) {
documentPicker.delegate = self
present(documentPicker, animated: true, completion: nil)
}
}
【问题讨论】:
标签: swift csv-import