加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift’String.Type’没有名为’stringWithContentsOfFile’的成

发布时间:2020-12-14 05:34:49 所属栏目:百科 来源:网络整理
导读:我无法从 Swift的.text文件中读取文本字符串. 我可以使用以下代码来设置文件 var writeError: NSError?let theFileToBeWritten = theStringWillBeSaved.writeToFile(pathToTheFile,atomically: true,encoding: NSUTF8StringEncoding,error: writeError); 但
我无法从 Swift的.text文件中读取文本字符串.

我可以使用以下代码来设置文件

var writeError: NSError?
let theFileToBeWritten = theStringWillBeSaved.writeToFile(pathToTheFile,atomically: true,encoding: NSUTF8StringEncoding,error: &writeError);

但是每当我尝试使用“String.stringWithContentsOfFile”读取文件时,我得到“’String.Type’没有名为’stringWithContentsOfFile’的成员. stringWithContentsOfFile也不会显示在自动完成中.

我正在使用Xcode 6.1 GM Seed 2.

我看到有人使用“stringWithContentsOfFile”从许多教程和堆栈溢出文件中读取文本,但为什么它不适用于我?

尝试这样的东西:
var error:NSError?
let string = String(contentsOfFile: "/usr/temp.txt",encoding:NSUTF8StringEncoding,error: &error)
if let theError = error {
   print("(theError.localizedDescription)")
}

Swift 2.2:

if let string = try? String(contentsOfFile: "/usr/temp.txt") {
  // Do something with string
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读