title: Best practice to implement a failable initializer in Swift
body: <p>With the following code I try to define a simple model class and it's failable initializer, which takes a (json-) dictionary as parameter. The initializer should return <code>nil</code> if the user name is not defined in the original json.  </p>
  
  <p>1.
  Why doesn't the code compile? The error message says: </p>
  
  <blockquote>
    <p>All stored properties of a class instance must be initialized before returning nil from an initializer.</p>
  </blockquote>
  
  <p>That doesn't make sense. Why should I initialize those properties when I plan to return <code>nil</code>?</p>
  
  <p>2.
  Is my approach the right one or would there be other ideas or common patterns to achieve my goal?</p>
  
  <pre><code>class User: NSObject {
  
      let userName: String
      let isSuperUser: Bool = false
      let someDetails: [String]?
  
      init?(dictionary: NSDictionary) {
          if let value: String = dictionary["user_name"] as? String {
              userName = value
          }
          else {
             return nil
          }
  
          if let value: Bool = dictionary["super_user"] as? Bool {
              isSuperUser = value
          }
  
          someDetails = dictionary["some_details"] as? Array
  
          super.init()
      }
  }
  </code></pre>