将多行制表符分隔文本转换为Array Swift 4

问题描述 投票:0回答:1

我需要读取一个文本文件并将其转换为数组,以便它可以用于填充tableView。数组行中的每个键都将进入tableView单元格中的不同标签。

制表符分隔的文本数据结构看起来像这样,但有120行: 文本文本文本文本

“TextA”进入labelA,“TextB”进入标签,“Text”进入标签S,依此类推。

我有一些工作。我可以读取文本文件并按行分隔。我遇到问题的部分是用标签分隔每一行,然后用键将它们放入数组中。 for循环中的部分是我遇到麻烦的地方。我不确定这是不是最好的方法。

我想我可以弄清楚如何将数组解析到tableview中,但我也可能会对它有所帮助。

谢谢您的帮助。

    var figureArray = [String]()

    let DocumentDirURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
    let fileURL = DocumentDirURL.appendingPathComponent(gTheCollection).appendingPathExtension("txt")

    do {
        let readText = try String(contentsOf: fileURL, encoding: String.Encoding.utf8)

        let theLines = readText.components(separatedBy: "\n")

        let theCount = theLines.count - 1
        for i in 0...(theCount) {

            let figureData = theLines[i].components(separatedBy: "\t")

            figureArray.append(figureData[0])
            figureArray.append(figureData[1])
            figureArray.append(figureData[2])
            figureArray.append(figureData[3])
        }

    } catch let error as NSError {
            print("Failed reading from URL: \(fileURL), Error: " + error.localizedDescription)
    }
arrays xcode10 swift4.2
1个回答
0
投票

我最终这样做了。

func buildTheDictionary() {
    let DocumentDirURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
    let fileURL = DocumentDirURL.appendingPathComponent(gTheCollection).appendingPathExtension("txt")

    figureArray = []

    do {
        let readText = try String(contentsOf: fileURL, encoding: String.Encoding.utf8)

        let theLines = readText.components(separatedBy: "\n") as [String]

        for i in 1..<theLines.count {

            let figureData = theLines[i].components(separatedBy: "\t") as [String]

            figureDict["obrien"] = figureData[0] //"\(figureData[0])"
            figureDict["manuf"] = figureData[1] //"\(figureData[1])"
            figureDict["descript"] = figureData[2] //"\(figureData[2])"
            figureDict["notes"] = figureData[3] //"\(figureData[3])"

            figureArray.addObjects(from: [figureDict])
        }

    } catch let error as NSError {
        print("Failed reading from URL: \(fileURL), Error: " + error.localizedDescription)
    }
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let theCell = tableView.dequeueReusableCell(withIdentifier: "figureList_Cell", for: indexPath) as! FigureList_Cell

    let figures = figureArray[indexPath.row]

    theCell.obrienLabel.text = (((figures as AnyObject) .object(forKey: "obrien") ?? "") as! String)
    theCell.manufLabel.text = (((figures as AnyObject) .object(forKey: "manuf") ?? "") as! String)
    theCell.descriptionLabel.text = (((figures as AnyObject) .object(forKey: "descript") ?? "") as! String)
    theCell.notesLabel.text = (((figures as AnyObject) .object(forKey: "notes") ?? "") as! String)


    return theCell
}
© www.soinside.com 2019 - 2024. All rights reserved.