mercredi 26 avril 2017

if let a = b, a.something() else

I have a code snippet here that works but am curious if there is a cleaner way to accomplish the same thing. I haven't seen anything exactly like this so far.

Logic I want to Achieve

  • The error is nil or is not a SpecialError
  • The error is non-nil BUT .foo() returns false

Code

enum SpecialError: Error {
    func foo() -> Bool
}

let error: Error? // Some optional Error is passed in

if let myError = error as? SpecialError, myError.foo() {
    // Don't care about this case
} else {
    // This is the case I'm interested in
    bar()
}

I'm curious if there is a better way to accomplish this if let else logic.

Aucun commentaire:

Enregistrer un commentaire