如何从磁盘上的文本文件中引入数据而不将其添加到项目中?

时间:2017-03-18 01:54:42

标签: swift xcode swift3 xcode8

我正在尝试从磁盘读取文件并将其数据解析为一种很好的格式。但是,该功能没有返回任何内容。它返回一个空数组。这是为什么?

注意:我一直在修补这个问题,我已经设法将之前的2个功能简化为这个。

功能:

func openFile(_ fileName:String, _ fileType:String) -> [(Double, Double)] {

        let file = fileName + fileType //this is the file. we will write to and read from it

        var text2: String = ""

        if let dir = FileManager.default.urls(for: .downloadsDirectory, in: .userDomainMask).first {

            let path = dir.appendingPathComponent(file)

            //reading
            do {
                text2 = try String(contentsOf: path, encoding: String.Encoding.utf8)
            }
            catch {/* error handling here */}


        }

        var pairs = [(Double,Double)]()
        var words: [String] = []

        for line in (text2.components(separatedBy: "\n").dropFirst()){
            if line != "" {
                words = line.components(separatedBy: "\t")
                pairs.append((Double(words[0])!,Double(words[1])!))

            }
        }

        return pairs
    }

0 个答案:

没有答案