Last active
April 29, 2020 12:32
-
-
Save bleft/d733562a649e03d073cd707a64df4b43 to your computer and use it in GitHub Desktop.
custom enum errors with localizedDescription
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// sepcifiy your errors | |
enum MyCustomError : Error { | |
case customError | |
case missingProperty | |
var localizedDescription: String { | |
switch self { | |
case .urlCouldNotBeCreated: | |
return NSLocalizedString("MyCustomErrorCcustomError", value: "a localized error message", comment: "") | |
case .missingProperty: | |
return NSLocalizedString("MyCustomErrorMissingProperty", value: "Missing coordinates", comment: "") | |
} | |
} | |
} | |
// use your error | |
func demo() throws { | |
throw MyCustomError.customError | |
} | |
// catch your error | |
do { | |
try demo() | |
} catch let error as MyCustomError { | |
print(error.localizedDescription) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
According to https://github.com/apple/swift-evolution/blob/master/proposals/0112-nserror-bridging.md
You need something like: