How to check for only errors in swift result? [duplicate]
Swift has this result enum;
enum Result<Success, Failure> where Failure : Error {
/// A success, storing a `Success` value.
case success(Success)
/// A failure, storing a `Failure` value.
case failure(Failure)
}
and I don't know how to only check for errors.
Lets say I load an image into an ImageView and it returns a result, but I don't need the .success(let _)
and I only want to check for .failure(let error)
:
{ result in
switch result {
case .success(let _):
print("I DONT NEED SUCCESS")
case .failure(let error):
print("ERROR")
}
}
Solution 1:
You can use if case
instead of a switch if you only want to pattern match a single case of an enum
rather than matching all cases.
{ result in
if case .failure(let error) = result {
print("Error: \(error)")
}
}