Created
February 25, 2020 14:07
-
-
Save endavid/97119ef05ba7dfbafec1a26dc77bdf11 to your computer and use it in GitHub Desktop.
#swift enum example for #medium
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
enum SerializationError: Error { | |
case missing(String) | |
case invalid(String, Any) | |
} | |
// in some deserialization code | |
// ... | |
guard let vertexData = json["vertices"] as? [NSNumber] else { | |
throw SerializationError.missing("vertices") | |
} | |
// ... | |
// handling errors with different associated values | |
switch serializationError { | |
case .missing(let what): | |
print("\(what) is missing") | |
case .invalid(let what, let someObject): | |
print("\(what) is invalid: \(someObject)") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment