代码之家  ›  专栏  ›  技术社区  ›  Quailcreek

将多行制表符分隔文本转换为数组swift 4

  •  0
  • Quailcreek  · 技术社区  · 7 年前

    我需要读取一个文本文件并将其转换成一个数组,这样它就可以用来填充一个TabLVIEW。数组行中的每个键将进入TableView单元格中的不同标签。

    制表符分隔的文本数据结构如下所示,但有120行:
    文本A文本B文本C文本D

    “texta”进入labela,“textb”进入labelb,“textc”进入labelc等等。

    我有一些这样的工作。我可以阅读文本文件并用线分隔。我遇到的麻烦是用制表符分隔每一行,并用键将其放入数组中。for循环中的部分是我遇到麻烦的地方。我不确定这是否是最好的方法。

    我想我可以知道如何将数组解析成TabLVIEW,但我也可能会提供帮助。

    谢谢你的帮助。

        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)
        }
    
    0 回复  |  直到 7 年前
        1
  •  0
  •   Quailcreek    7 年前

    我最后做了这个。

    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
    }
    
    推荐文章