title: Best practice for Swift methods that can return or error
body: <p>I’m practicing Swift and have a scenario (and a method) where the result could either be successful or a failure.</p>
  
  <p>It’s a security service class. I have a method where I can authenticate with an email address and password, and want to either return a <code>User</code> instance if the credentials are correct, or throw some form of <code>false</code> value.</p>
  
  <p>I’m a bit confused as my understanding of Swift methods is you need to specify a return type, so I have:</p>
  
  
  
  <pre class="lang-swift prettyprint-override"><code>class SecurityService {
      static func loginWith(email: String, password: String) -&gt; User {
          // Body
      }
  }
  </code></pre>
  
  <p>I’ve seen in Go and Node.js methods that return a “double” value where the first represents any errors, and the second is the “success” response. I also know that Swift doesn’t have things like errors or exceptions (but that may have changed since as I was learning an early version of Swift).</p>
  
  <p>What would be the appropriate thing to do in this scenario?</p>