Ios xcode7beta给出了错误消息。
它以前工作过。但是在Xcode 7测试版中给出了错误。请帮帮我Ios xcode7beta给出了错误消息。,ios,nsstring,try-catch,swift2,xcode7,Ios,Nsstring,Try Catch,Swift2,Xcode7,它以前工作过。但是在Xcode 7测试版中给出了错误。请帮帮我 private func htmlStringWithFilePath(path: String) -> String? { // Error optional for error handling var error: NSError? // Get HTML string from path let htmlString = NSString(conten
private func htmlStringWithFilePath(path: String) -> String? {
// Error optional for error handling
var error: NSError?
// Get HTML string from path
let htmlString = NSString(contentsOfFile: path, encoding: NSUTF8StringEncoding, error: &error)
// Check for error
if let error = error {
printLog("Lookup error: no HTML file found for path, \(path)")
}
return htmlString! as String
}
现在给出2个错误
在Swift 2中,有一个新的错误处理模型,带有try-and-catch(几乎在整个基金会/Cocoa中)。以下是一个工作示例:
private func htmlStringWithFilePath(path: String) -> String? {
// Get HTML string from path
// you can also use String but in both cases the initializer throws
// so you need the do-try-catch syntax
do {
// use "try"
let htmlString = try String(contentsOfFile: path, encoding: NSUTF8StringEncoding)
// or directly return
return htmlString
// Check for error
} catch {
// an "error" variable is automatically created for you
// in Swift 2 print is now used instead of println.
// If you don't want a new line after the print, call: print("something", appendNewLine: false)
print("Lookup error: no HTML file found for path, \(path)")
return nil
}
}