Ios xcode7beta给出了错误消息。

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

它以前工作过。但是在Xcode 7测试版中给出了错误。请帮帮我

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个错误

  • 让htmlString=NSString(内容文件:路径,编码: NSUTF8StringEncoding,错误:&错误)错误找不到 接受参数列表的NSString类型的初始值设定项 类型(…)
  • printLog(“查找错误:找不到路径(path)”的HTML文件) 使用未解析标识符printlog时出错

  • 在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
        }
    }