title: Handling an Error with custom parameter in an if-statement in Swift
body: <p>I created a custom enum conforming to <code>Error</code> in Swift as such:</p>
  
  <pre><code>enum CustomError: Error{
      case errorWith(code: Int)
      case irrelevantError
  }
  </code></pre>
  
  <p><code>CustomError</code> can optionally be returned from an asynchronous function through a closure like so:</p>
  
  <pre><code>func possiblyReturnError(completion: (Error?) -&gt; ()){
      completion(CustomError.errorWith(code: 100))
  }
  </code></pre>
  
  <p>I would now like to check the type of <code>CustomError</code> that is returned in the closure. Along with that, if it is a <code>CustomError.errorWith(let code)</code>, would like to extract the code of that <code>CustomError.errorWith(let code)</code>. All of this I would like to be done using the condition of an if-statement. Along the lines of this:</p>
  
  <pre><code>{ (errorOrNil) in
      if let error = errorOrNil, error is CustomError, // check if it is an 
      //.errorWith(let code) and extract the code, if so
      {
          print(error)
      }
      else {
          print("The error is not a custom error with a code")
      }
  } 
  </code></pre>
  
  <p>Is this at all possible using Swift 3.0? I tried various combinations that I could think of, however, all attempts have been fruitless and ended in compilation time errors. </p>