niks290192 niks290192 - 5 months ago 9
Swift Question

Initialiser for conditional binding must have Optional type, not '[NSObject: AnyObject]' SwiftyJSON

i wanted to jump over to use Xcode 7.3.1 and convert my code, but I'm facing some kind of problem here,this is how i used to use it in Swift 1.1 but i am getting error -Initialiser for conditional binding must have Optional type, not '[NSObject: AnyObject]' SwiftyJSON:

if let userInfo = error.userInfo { <-- Error in this line //Initialiser for conditional binding must have Optional type, not '[NSObject: AnyObject]'
if let breadcrumb = userInfo["JSONErrorBreadCrumbKey"] as? NSString {
let newBreadCrumb = (breadcrumb as String) + "/\(index)"
let newUserInfo = [NSLocalizedDescriptionKey: "JSON Keypath Error: Incorrect Keypath \"\(newBreadCrumb)\"",
"JSONErrorBreadCrumbKey": newBreadCrumb]
return JSONValue.JInvalid(NSError(domain: "JSONErrorDomain", code: 1002, userInfo: newUserInfo))
}
}


Thanks in Advance!

Answer

Conditional binding (if let) expects an expression where the result can be nil. That error means that error.userInfo is never nil.

You just have to delete the if in the first line, like this:

let userInfo = error.userInfo

The rest of your code should be fine. You also need to delete the closing bracket } of that if as well.