Reputation: 117
it was working before. but give error in Xcode 7 Beta. Help me please
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
}
now gives 2 error.
Upvotes: 0
Views: 359
Reputation: 13243
In Swift 2 there is a new error handling model with try and catch (almost throughout the Foundation/Cocoa). Here is a working example:
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
}
}
Upvotes: 0