title: Swift - Fetch method with Generics
body: <p>the Idea is to fetch "Typed" data using Coredata.</p>
  
  <p><strong>Updated</strong></p>
  
  <pre><code>class func retrieve&lt;T: AnyObject&gt;(entityName:T.Type, sortBy:String? = nil, isAscending:Bool? = true, predicate:NSPredicate? = nil) -&gt; AnyObject[]    {
  
      println("\(entityName)")
      let request = NSFetchRequest(entityName: "Users")
      request.returnsObjectsAsFaults = false
      if predicate != nil {
          request.predicate = predicate
      }
  
      if (sortBy != nil){
          var sorter: NSSortDescriptor = NSSortDescriptor(key: sortBy , ascending: isAscending!)
          request.sortDescriptors = [sorter]
      }
  
  
      var error: NSError? = nil
      var fetchedResult = myDataModel.managedObjectContext.executeFetchRequest(request, error: &amp;error)
      if !error {
          println("errore: \(error)")
      }
  
  
      println("retrieved \(fetchedResult.count) elements for \(entityName)")
      return fetchedResult
  
  }
  </code></pre>
  
  <p>now, what I wrote above is syntactically correct but I believe that there is also another way to make the same, maybe more 'easily'. I think the propose is clear :)
  I was trying to get Class name from <em>entityName</em> input param</p>
  
  <p>an to call this function in the way below or something similar :</p>
  
  <pre><code> var xx = myDataModel.retrieve(Users.self)
  </code></pre>
  
  <p>where</p>
  
  <pre><code> Users: NSManagedeObject
  </code></pre>
  
  <p>do you have any suggestion?</p>