If I have a throwing method, like this:
func doSomethingWithString(string:String) throws { guard string.characters.count > 0 else { throw NSError(domain: "CustomErrorDomain", code: 42, userInfo: ["foo" : "bar"]) }
Then I will try to call it and read userInfo :
do { try doSomethingWithString("") } catch let error as NSError { print(error.domain) print(error.code) print(error.userInfo) }
... it returns as an empty dictionary (but the domain and code are correctly filled in):
CustomErrorDomain 42 [:]
But if I add this extra step:
do { try doSomethingWithString("") } catch let e { let error = e as NSError print(error.domain) print(error.code) print(error.userInfo) }
... it works:
CustomErrorDomain 42 [foo: bar]
Does anyone know why this could be?
FYI - I'm on Xcode 7 beta 2 (7A121l)
swift swift2 error-handling
ganzogo
source share