【问题标题】:Convert multi-line tab separated text to an Array Swift 4将多行制表符分隔的文本转换为数组 Swift 4
【发布时间】:2019-02-24 02:21:22
【问题描述】:

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

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

“TextA”进入labelA,“TextB”进入labelB,“TextC”进入labelC等等。

我有一些这样的工作。我可以阅读文本文件并按行分隔。我遇到问题的部分是用制表符分隔每一行并用键将其放入数组中。 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)
    }

【问题讨论】:

  • 您的问题到底是什么? let figureData = theLines[i].components(separatedBy: "\t") 行不行?
  • 是的,让 figureData = theLines[i].components(separatedBy: "\t") 确实有效。它将标签处的刺分开,但它只是使标签变成逗号。如果我打印 theLines 我得到: ["TextA\tTextB\tTextC\tTextD", "TextE\tTextF\tTextG\tTextH"] 如果我在代码运行后打印 figureArray,我得到: ["TextA", "TextB", " TextC”、“TextD”、“TextE”、“TextF”、“TextG”、“TextH”] 我认为问题在于我如何附加 figureArray。抱歉,我在解释问题时遇到了一些问题。
  • ["TextA", "TextB", ... , "TextH"] (逗号分隔值)是 Swift 在终端中打印数组时使用的格式,所以听起来像你的代码实际上正在做你所期望的。

标签: arrays xcode10 swift4.2


【解决方案1】:

我最终这样做了。

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
}

【讨论】:

    猜你喜欢
    • 2017-05-13
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 2013-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-07
    相关资源
    最近更新 更多