第1年4月25日 swift - 从指定路径的文件中读取数据

1.swift - 从指定路径的文件中读取数据

func readPathLossFile(_ path: String) -> [[Substring]]? {
        let fileManager = FileManager.default
        if let url = URL.init(string: path) {
            if fileManager.fileExists(atPath: url.path) {
                let txtData = fileManager.contents(atPath: url.path)
                var dataArray:[[Substring]] = []
                if txtData == nil {
                    return nil
                }
                let readString = String(data: txtData!, encoding: String.Encoding.utf8)
                print("readString: \(String(describing: readString))")
                if readString!.contains("\r\n") {
                    let dataOfRowArray = readString?.split(separator: "\r\n")
                    for (_,rowString) in dataOfRowArray!.enumerated() {
                        let rowArray = rowString.split(separator: ",")
                        if rowArray.count > 0 {
                            dataArray.append(rowArray)
                        }
                    }
                } else {
                    let dataOfRowArray = readString?.split(separator: "\r")
                    for (_,rowString) in dataOfRowArray!.enumerated() {
                        let rowArray = rowString.split(separator: ",")
                        if rowArray.count > 0 {
                            dataArray.append(rowArray)
                        }
                    }
                }
                return dataArray
            }else {
                print("Path loss file is not exists")
                return nil
            }
        }
        return []
    }

 

https://blog.csdn.net/u011598999/article/details/80108598

posted @ 2021-04-25 21:03  lianhuaren  阅读(118)  评论(0编辑  收藏  举报