Xcode 7.1 beta:文件内容错误 [英] Xcode 7.1 beta: Content Of File Error
本文介绍了Xcode 7.1 beta:文件内容错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我刚刚完成了我的 swift 应用程序的最后润色.但是在升级到 Beta 7 之后,它给了我ContentOfFile"字符串的错误.谁能帮我了解如何解决这个问题?
I had just finished the final touches to my swift app. But after upgrading to Beta 7 its giving me errors for the 'ContentOfFile' String. can anyone help me understand how I can go about fixing this please?
这是我的自动取款机.
//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,编码:UInt,错误:NilLiteralConvertible)"的参数列表调用类型String"的初始化程序
Error Message: Cannot invoke initializer for type 'String' with an argument list of type '(contentsOfFile: String, encoding: UInt, error: NilLiteralConvertible)'
推荐答案
你需要实现 do try catch 错误处理.试试这个:
You need to implement do try catch error handling. Try like this:
编辑/更新:
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)
}
}
这篇关于Xcode 7.1 beta:文件内容错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文