安全处理 Swift 中的 Apple TabularData table 数据
Safe handling of Apple TabularData table data in Swift
我已经使用 DataFrame 成功导入了一个简单的两列 CSV 文件。
现在我想将每行中的两个单元格变成字符串。
左侧或右侧偶尔会缺失一个值;见打印输出。
table
当我尝试处理包含 nil 单元格的行时,程序崩溃并显示 “致命错误:在展开可选值时意外发现 nil”
所以我的问题是,如何安全地将单元格转换为字符串(如果为 Nil,则为“”)?
完成下面的 ContentView 代码,在此先感谢您的帮助。
import SwiftUI
import TabularData
struct ContentView: View {
@State var openFile = false
var body: some View {
VStack {
Button(action: {openFile.toggle()}, label: {
Text("Open")
})
}.fileImporter(
isPresented: $openFile,
allowedContentTypes: [.commaSeparatedText],
allowsMultipleSelection: false) { (result) in
do {
let fileURL = try result.get().first
if fileURL!.startAccessingSecurityScopedResource() {
print(fileURL!)
importTable(url: fileURL!)
}
fileURL!.stopAccessingSecurityScopedResource()
} catch {
print("Unable to read file contents")
print(error.localizedDescription)
}
}
}
func importTable(url: URL) {
var importerTable: DataFrame = [:]
let options = CSVReadingOptions(hasHeaderRow: false, delimiter: ",")
do {
importerTable = try DataFrame(
contentsOfCSVFile: url,
options: options)
} catch {
print("ERROR reading CSV file")
print(error.localizedDescription)
}
print("\(importerTable)")
importerTable.rows.forEach { row in
let leftString = row[0]! as! String
let rightString = row[1]! as! String
print("Left: \(leftString) Right: \(rightString)")
}
}
}
fileImporter
上下文非常不安全,任何不小心写的感叹号都可能导致应用程序崩溃。
首先,您必须检查 fileURL
,如果是 nil
,则中止导入
guard let fileURL = try result.get().first else { return }
if fileURL.startAccessingSecurityScopedResource() {
print(fileURL)
importTable(url: fileURL)
}
fileURL.stopAccessingSecurityScopedResource()
在 importTable
中,如果抛出错误,则在 do - catch
后不得继续,并且必须检查 CSV 文件是否每行包含三个项目。
func importTable(url: URL) {
var importerTable: DataFrame = [:]
let options = CSVReadingOptions(hasHeaderRow: false, delimiter: ",")
do {
importerTable = try DataFrame(
contentsOfCSVFile: url,
options: options)
print("\(importerTable)")
importerTable.rows.forEach { row in
if row.count > 2 {
let leftString = row[1] as! String
let rightString = row[2] as! String
print("Left: \(leftString) Right: \(rightString)")
}
}
} catch {
print("ERROR reading CSV file")
print(error.localizedDescription)
}
}
我已经使用 DataFrame 成功导入了一个简单的两列 CSV 文件。 现在我想将每行中的两个单元格变成字符串。 左侧或右侧偶尔会缺失一个值;见打印输出。 table
当我尝试处理包含 nil 单元格的行时,程序崩溃并显示 “致命错误:在展开可选值时意外发现 nil”
所以我的问题是,如何安全地将单元格转换为字符串(如果为 Nil,则为“”)?
完成下面的 ContentView 代码,在此先感谢您的帮助。
import SwiftUI
import TabularData
struct ContentView: View {
@State var openFile = false
var body: some View {
VStack {
Button(action: {openFile.toggle()}, label: {
Text("Open")
})
}.fileImporter(
isPresented: $openFile,
allowedContentTypes: [.commaSeparatedText],
allowsMultipleSelection: false) { (result) in
do {
let fileURL = try result.get().first
if fileURL!.startAccessingSecurityScopedResource() {
print(fileURL!)
importTable(url: fileURL!)
}
fileURL!.stopAccessingSecurityScopedResource()
} catch {
print("Unable to read file contents")
print(error.localizedDescription)
}
}
}
func importTable(url: URL) {
var importerTable: DataFrame = [:]
let options = CSVReadingOptions(hasHeaderRow: false, delimiter: ",")
do {
importerTable = try DataFrame(
contentsOfCSVFile: url,
options: options)
} catch {
print("ERROR reading CSV file")
print(error.localizedDescription)
}
print("\(importerTable)")
importerTable.rows.forEach { row in
let leftString = row[0]! as! String
let rightString = row[1]! as! String
print("Left: \(leftString) Right: \(rightString)")
}
}
}
fileImporter
上下文非常不安全,任何不小心写的感叹号都可能导致应用程序崩溃。
首先,您必须检查 fileURL
,如果是 nil
guard let fileURL = try result.get().first else { return }
if fileURL.startAccessingSecurityScopedResource() {
print(fileURL)
importTable(url: fileURL)
}
fileURL.stopAccessingSecurityScopedResource()
在 importTable
中,如果抛出错误,则在 do - catch
后不得继续,并且必须检查 CSV 文件是否每行包含三个项目。
func importTable(url: URL) {
var importerTable: DataFrame = [:]
let options = CSVReadingOptions(hasHeaderRow: false, delimiter: ",")
do {
importerTable = try DataFrame(
contentsOfCSVFile: url,
options: options)
print("\(importerTable)")
importerTable.rows.forEach { row in
if row.count > 2 {
let leftString = row[1] as! String
let rightString = row[2] as! String
print("Left: \(leftString) Right: \(rightString)")
}
}
} catch {
print("ERROR reading CSV file")
print(error.localizedDescription)
}
}