我正在使用 Xcode 和 swift。我正在嘗試創建一個應用程式來自動化收據檔案,而不是傳統上在電子表格中填寫資訊。
我使用了視覺框架來提取收據資料并對其進行分類。我能夠對基本資訊(價格、日期等)進行分類,但我正在努力解決如何在檔案中存盤和顯示資料。
我研究了我可以使用的不同資料庫,但我想知道是否有一種捷徑可以使用 swift 將這些資料從應用程式直接上傳到 Google 電子表格。簡而言之,即使我將使用資料庫方法,我仍然需要以 Excel 或 CSV 方式顯示資料,我不確定什么是一個好的方法。有任何想法嗎?
uj5u.com熱心網友回復:
您可以使用 sql 資料庫,但如果您需要存盤資料,Core Data 也非常棒。特別是如果您需要將它們與 iCloud 同步。過去我一直使用 sql,但現在我發現 CoreData 更好、更干凈、更容易(經過一段時間學習如何使用它)。
關于 CSV,您可以使用以下代碼示例匯出資料模型:
// MARK: - Exoprt to CSV
@IBAction func selectorexport(toCSV sender: Any) {
let docPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).map(\.path)[0]
let filePath = URL(fileURLWithPath: docPath).appendingPathComponent("file.csv").path
let contents = String(repeating: "\0", count: 0)
//fill contents with data in csv format
// ...
//var error: Error?
do {
try contents.write(
toFile: filePath,
atomically: true,
encoding: .utf8)
} catch {
}
// check for the error
let fileUrl = URL(fileURLWithPath: filePath)
let activityItems = ["file.csv", fileUrl] as [Any]
let activityViewController = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)
if let popoverController = activityViewController.popoverPresentationController {
popoverController.barButtonItem = btShare
popoverController.permittedArrowDirections = []
}
// Present action sheet.
present(activityViewController, animated: true)
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/493467.html
