gpt4 book ai didi

swift - Xcode 7.1 测试版 : Content Of File Error

转载 作者:行者123 更新时间:2023-11-30 10:11:44 26 4
gpt4 key购买 nike

我刚刚完成了我的 swift 应用程序的最后修改。但升级到 Beta 7 后,它给了我“ContentOfFile”字符串错误。谁能帮我理解如何解决这个问题?

这是我的 ATM 机。

//Reads the Text File
if var path = NSBundle.mainBundle().pathForResource("Chapters", ofType: "txt"){

//Reads the Text File into one Huge String
var data = String(contentsOfFile:path, encoding: NSUTF8StringEncoding, error: nil)

//sets String content of the Text File as an Array. With each string start at \n (new line)
if var content = (data){

//from the mass string of data from the text file, Each chapter content is seperated by #
var Chapters: [String] = content.componentsSeparatedByString("@")

//without removing index in the beginning there will be an extra element printed in the array.
Chapters.removeAtIndex(0)

错误消息:无法使用类型为“(contentsOfFile: String,encoding:UInt,error:NilLiteralConvertible)”的参数列表调用类型“String”的初始值设定项

最佳答案

您需要实现 do try catch 错误处理。尝试这样:

编辑/更新:

Swift 3 或更高版本

if let fileURL = Bundle.main.url(forResource: "Chapters", withExtension: "txt") {
do {
let string = try String(contentsOf: fileURL, encoding: .utf8)
var chapters = string.components(separatedBy: "@")
chapters.removeFirst()
} catch {
print(error)
}
}

关于swift - Xcode 7.1 测试版 : Content Of File Error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32598208/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com