我的Xcode项目中有以下文本文件:
Section Designation,Mass per metre (kg/m),Depth of section h (mm),Width of section b (mm),Web thickness tw (mm),Flange thickness tf (mm),Root radius r (mm),Depth between fillets d (mm)
1016x305x584,584.0,1056.0,314.0,36.0,64.0,30.0,868.1
1016x305x494,494.0,1036.0,309.0,31.0,54.0,30.0,868.1
1016x305x438,438.0,1026.0,305.0,26.9,49.0,30.0,868.1
1016x305x415,415.0,1020.0,304.0,26.0,46.0,30.0,868.1
1016x305x393,392.7,1015.9,303.0,24.4,43.9,30.0,868.1
1016x305x350,350.0,1008.0,302.0,21.1,40.0,30.0,868.1
1016x305x314,314.3,999.9,300.0,19.1,35.9,30.0,868.1
1016x305x272,272.3,990.1,300.0,16.5,31.0,30.0,868.1
1016x305x249,248.7,980.1,300.0,16.5,26.0,30.0,868.1
1016x305x222,222.0,970.3,300.0,16.0,21.1,30.0,868.1
然后,我在ViewController中编写了以下代码,以解析数据并将其制成可用的数组,我打算稍后将其用于填充UITable:
class UniversalBeamsViewController: UIViewController {
var data:[[String:String]] = []
var columnTitles:[String] = []
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
convertCSV(file: "data")
}
func readDataFromFile(file:String) -> String!{
guard let filepath = Bundle.main.path(forResource: file, ofType: "txt")
else {
return nil
}
do {
let contents = try String(contentsOfFile: filepath)
return contents
} catch {
print("File Read Error for file \(filepath)")
return nil
}
}
func cleanRows(file: String)->String{
var cleanFile = readDataFromFile(file: file)
cleanFile = cleanFile!.replacingOccurrences(of: "\r", with: "\n")
cleanFile = cleanFile!.replacingOccurrences(of: "\n\n", with: "\n")
return cleanFile!
}
func getStringFieldsForRow(row:String, delimiter:String)-> [String]{
return row.components(separatedBy: delimiter)
}
func convertCSV(file:String){
let rows = cleanRows(file: file).components(separatedBy: "\n")
if rows.count > 0 {
data = []
columnTitles = getStringFieldsForRow(row: rows.first!,delimiter:",")
for row in rows {
let fields = getStringFieldsForRow(row: row,delimiter: ",")
if fields.count != columnTitles.count {continue}
var dataRow = [String:String]()
for (index,field) in fields.enumerated(){
let fieldName = columnTitles[index]
dataRow[fieldName] = field
}
data += [dataRow]
}
} else {
print("No data in file")
}
print(data)
}
}
但是,当执行convertCSV方法时,我得到以下作为数组输出:
[["Web thickness tw (mm)": "Web thickness tw (mm)", "Depth of section h (mm)": "Depth of section h (mm)", "Flange thickness tf (mm)": "Flange thickness tf (mm)", "Section Designation": "Section Designation", "Mass per metre (kg/m)": "Mass per metre (kg/m)", "Width of section b (mm)": "Width of section b (mm)", "Depth between fillets d (mm)": "Depth between fillets d (mm)", "Root radius r (mm)": "Root radius r (mm)"], ["Web thickness tw (mm)": "36.0", "Depth of section h (mm)": "1056.0", "Root radius r (mm)": "30.0", "Depth between fillets d (mm)": "868.1", "Width of section b (mm)": "314.0", "Mass per metre (kg/m)": "584.0", "Section Designation": "1016x305x584", "Flange thickness tf (mm)": "64.0"], ["Depth between fillets d (mm)": "868.1", "Flange thickness tf (mm)": "54.0", "Web thickness tw (mm)": "31.0", "Mass per metre (kg/m)": "494.0", "Section Designation": "1016x305x494", "Depth of section h (mm)": "1036.0", "Root radius r (mm)": "30.0", "Width of section b (mm)": "309.0"], ["Section Designation": "1016x305x438", "Depth between fillets d (mm)": "868.1", "Width of section b (mm)": "305.0", "Web thickness tw (mm)": "26.9", "Root radius r (mm)": "30.0", "Flange thickness tf (mm)": "49.0", "Depth of section h (mm)": "1026.0", "Mass per metre (kg/m)": "438.0"], ["Depth of section h (mm)": "1020.0", "Section Designation": "1016x305x415", "Web thickness tw (mm)": "26.0", "Mass per metre (kg/m)": "415.0", "Width of section b (mm)": "304.0", "Flange thickness tf (mm)": "46.0", "Root radius r (mm)": "30.0", "Depth between fillets d (mm)": "868.1"], ["Flange thickness tf (mm)": "43.9", "Root radius r (mm)": "30.0", "Mass per metre (kg/m)": "392.7", "Width of section b (mm)": "303.0", "Depth of section h (mm)": "1015.9", "Web thickness tw (mm)": "24.4", "Depth between fillets d (mm)": "868.1", "Section Designation": "1016x305x393"], ["Web thickness tw (mm)": "21.1", "Depth of section h (mm)": "1008.0", "Depth between fillets d (mm)": "868.1", "Mass per metre (kg/m)": "350.0", "Root radius r (mm)": "30.0", "Flange thickness tf (mm)": "40.0", "Section Designation": "1016x305x350", "Width of section b (mm)": "302.0"], ["Depth of section h (mm)": "999.9", "Width of section b (mm)": "300.0", "Web thickness tw (mm)": "19.1", "Root radius r (mm)": "30.0", "Depth between fillets d (mm)": "868.1", "Flange thickness tf (mm)": "35.9", "Section Designation": "1016x305x314", "Mass per metre (kg/m)": "314.3"], ["Web thickness tw (mm)": "16.5", "Mass per metre (kg/m)": "272.3", "Section Designation": "1016x305x272", "Depth of section h (mm)": "990.1", "Width of section b (mm)": "300.0", "Flange thickness tf (mm)": "31.0", "Root radius r (mm)": "30.0", "Depth between fillets d (mm)": "868.1"], ["Mass per metre (kg/m)": "248.7", "Root radius r (mm)": "30.0", "Depth of section h (mm)": "980.1", "Flange thickness tf (mm)": "26.0", "Web thickness tw (mm)": "16.5", "Depth between fillets d (mm)": "868.1", "Width of section b (mm)": "300.0", "Section Designation": "1016x305x249"], ["Depth between fillets d (mm)": "868.1", "Mass per metre (kg/m)": "222.0", "Flange thickness tf (mm)": "21.1", "Root radius r (mm)": "30.0", "Width of section b (mm)": "300.0", "Web thickness tw (mm)": "16.0", "Depth of section h (mm)": "970.3", "Section Designation": "1016x305x222"]]
从上面的输出中可以看出,列标题被打印在数组内部,并占据了大主数组中的整个第一个数组。我不希望将列标题作为我想要获得的大数组的一部分进行打印。你知道我需要修改代码的哪一部分来实现这一目标吗?
关于, 沙迪。
答案 0 :(得分:0)
您的循环
for row in rows
将包括所有行,包括第一个具有列名的行,解决此问题的一种方法是对索引进行迭代,但从1开始而不是0
for i in 1..<rows.count {
let row = rows[i]
// same as before...
答案 1 :(得分:0)
您要提取第一行的字段名称,然后迭代也包含第一行的 entire 数组。
使用基于索引的循环并从1(第二个索引)开始
func convertCSV(file:String){
let rows = cleanRows(file: file).components(separatedBy: "\n")
if !rows.isEmpty {
data = []
columnTitles = getStringFieldsForRow(row: rows.first!,delimiter:",")
for i in 1..<rows.count {
let fields = getStringFieldsForRow(row: rows[i],delimiter: ",")
if fields.count != columnTitles.count {continue}
var dataRow = [String:String]()
for (index,field) in fields.enumerated(){
let fieldName = columnTitles[index]
dataRow[fieldName] = field
}
data += [dataRow]
}
} else {
print("No data in file")
}
print(data)
}