ios 读取csv文件

最终代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
      
.fileImporter(isPresented: $imported, allowedContentTypes: [.delimitedText], allowsMultipleSelection: false){ result in
do {

guard let fileUrl: URL = try result.get().first else {return}

if (CFURLStartAccessingSecurityScopedResource(fileUrl as CFURL)) { //不在这个if里就出错,唉
//理由:iOS的沙盒机制保护需要我们申请临时调用url的权限

guard let data = String(data: try Data(contentsOf: fileUrl), encoding: .utf8) else { return }


handleSSJdataCSV(data: data)
//done accessing the url
CFURLStopAccessingSecurityScopedResource(fileUrl as CFURL)
}
else {
print("Permission error!")
}
} catch {
// Handle failure.
print ("error reading: \(error.localizedDescription)")
}
}

//数据格式处理代码
func handleSSJdataCSV(data : String){
var csvToStruct = [SSJdata]()

//split the long string into an array of "rows " of sata. each row is a string
//detect "/n" carriage return , then split
var rows = data.components(separatedBy: "\n")

let columnCount = rows.first?.components(separatedBy: ",").count
//remove the header rows
rows.removeFirst()

//loop around each row and split into columns
for row in rows{
let csvColumes = row.components(separatedBy: ",")
if csvColumes.count == columnCount{
let genericStruct = SSJdata.init( raw: csvColumes)
csvToStruct.append(genericStruct!)
}

}
print(csvToStruct)

for singleRecord in csvToStruct{
print(singleRecord.recordType)
}
//done accessing the url
}

参考文献:
https://stackoverflow.com/questions/67731694/how-do-i-save-an-imported-file-name-as-string-after-importing-file-in-swiftui

1
2
3
4
5
//不太管用
let fileUrl = try res.get()
self.fileName = fileUrl.lastPathComponent // <--- the file name you want

let fileData = try Data(contentsOf: fileUrl)

https://betterprogramming.pub/importing-and-exporting-files-in-swiftui-719086ec712
有大用
https://github.com/acwright/ImportExport
上面那个链接内容的示范工程

作者

孙起

发布于

2021-11-24

更新于

2021-11-24

许可协议

评论