contentsOfFile: - “使用未解析的标识符'contentsOfFile'

时间:2016-12-10 13:26:19

标签: ios swift

我正在尝试从名为CSV的简单data.txt文本文件中读取数据,该文件保存在我的主文件夹中。我收到错误“使用未解析的标识符'contentsOfFile',如代码所示

我哪里错了?

//MARK: Data reading and writing functions

func readDataFromFile(fileName:String) -> String!{
    guard let filepath = Bundle.main.path(forResource: fileName, ofType: "txt")

    else {return nil}
    do { //Error showing following line
        let contents = try String(contentsOfFile: filepath, encoding: String.Encoding.utf8)  

        return contents
    }catch{
        print ("File Read Error")
        return nil
    }
}

0 个答案:

没有答案