davudi
davudi

Reputation: 117

Xcode 7 beta gives error.

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.

  1. let htmlString = NSString(contentsOfFile: path, encoding: NSUTF8StringEncoding, error: &error) ERROR cannot find an initializer for type NSString that accepts an argument list of type(....)
  2. printLog("Lookup error: no HTML file found for path, (path)") ERROR use of unresolved identifier printlog

Upvotes: 0

Views: 359

Answers (1)

Qbyte
Qbyte

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

Related Questions