title: EXC_BAD_ACCESS using Generics in Swift
body: <p>Related question: <a href="https://stackoverflow.com/questions/27333657/generic-completion-handler-in-swift">Generic completion handler in Swift</a></p>
  
  <p>In a Swift app I'm writing, I'm downloading JSON and I want to convert it into model objects. Right now, I'm doing that like this:</p>
  
  <pre><code>func convertJSONData&lt;T: Entity&gt;(jsonData: NSData?, jsonKey: JSONKey, _: T.Type) -&gt; [T]? {
          var entities = [T]()
          if let data = jsonData {
  
              // Left out error checking for brevity
  
              var json = JSON(data: data, options: nil, error: nil)
              var entitiesJSON = json[jsonKey.rawValue]
  
              for (index: String, subJson: JSON) in entitiesJSON {
  
                  // Error: EXC_BAD_ACCESS(code=EXC_I386_GPFLT)
  
                  let entity = T(json: subJson)
                  entities.append(entity)
              }
          }
          return entities
      }
  </code></pre>
  
  <p>Each object conforming to <code>Entity</code> implements <code>init(json: JSON)</code>. <code>JSON</code> is a type defined in the <a href="https://github.com/SwiftyJSON/SwiftyJSON" rel="nofollow noreferrer">SwiftyJSON</a> library. That's also the reason the enumeration looks a bit weird.</p>
  
  <p>I call <code>convertJSONData()</code> in this method:</p>
  
  <pre><code>public func performJSONRequest&lt;T where T: Entity&gt;(jsonRequest: JSONRequest&lt;T&gt;) {
          var urlString = ...
          Alamofire.request(.GET, urlString, parameters: nil, encoding: .JSON).response { (request, response, data, error) -&gt; Void in
                  var books = self.convertJSONData(data as? NSData, jsonKey: jsonRequest.jsonKey, T.self)
                  jsonRequest.completionHandler(books, error)
          }
      }
  </code></pre>
  
  <p>I get a runtime <code>EXC_BAD_ACCESS(code=EXC_I386_GPFLT)</code> error calling <code>T(json: subJSON)</code>. There are no compiler warnings or errors. Although I left out error checking in the above code, there is error checking in the actual code and <code>error</code> is nil.</p>
  
  <p>I'm not sure whether this is a compiler bug or my fault and any help figuring that out is much appreciated.</p>