title: How do you fetch NSSet from NSManagedObject in Swift?
body: <p>I have a one to many relationship from <code>Set</code> to <code>Card</code> for a basic Flashcard App modelled in my Core Data.</p>
  
  <p>Each <code>Set</code> has a set name, set description, and a relationships many card1s. Each <code>Card1</code> has a front, back, and photo. In my table view, I've managed to retrieve all saved <code>Set</code>s from core data and display them. Now I want to fetch each <code>Set</code>'s cards when a user clicks on the appropriate cell in my next view controller. </p>
  
  <p>This is my code for the table view controller:</p>
  
  <pre><code>// MARK: Properties
  var finalArray = [NSManagedObject]()
  
  override func viewDidLoad() {
      getAllSets()
      println(finalArray.count)
  }
  
  func getAllSets() {
      let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
      let managedContext = appDelegate.managedObjectContext!
      let fetchRequest = NSFetchRequest(entityName:"Set")
      var error: NSError?
      let fetchedResults = managedContext.executeFetchRequest(fetchRequest,error: &amp;error) as? [NSManagedObject]
      println("Am in the getCardSets()")
      if let results = fetchedResults {
          finalArray = results
          println(finalArray.count)
      }
      else {
          println("Could not fetch \(error), \(error!.userInfo)")
      }
  }
  
  // MARK: Displaying the data
  
  override func numberOfSectionsInTableView(tableView: UITableView) -&gt; Int {
       return 1
  }
  
  override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
      return finalArray.count
  }
  
  override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&gt; UITableViewCell {
      let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! SetTableViewCell
  
      let sets = finalArray[indexPath.row]
  
      cell.setName.text = sets.valueForKey("setName")as? String
      cell.setDescription.text = sets.valueForKey("setDescription")as? String
      return cell
  }
  
  override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
      if segue.identifier == "ShowDetail" {
          let dest = segue.destinationViewController as! Display
  
          // Get the cell that generated this segue.
          if let selectedCell = sender as? SetTableViewCell {
              let indexPath = tableView.indexPathForCell(selectedCell)!
  
              let selectedSet = finalArray[indexPath.row]
              dest.recievedSet = selectedSet
          }
      }
  }
  </code></pre>
  
  <p>In my destination view controller, how would I go about retrieving all the cards in that the <code>recievedSet</code>? I've tried converting the NSSet to an array and casting it to a <code>[Card1]</code> array but when I attempt to display the first Card1's front String property onto the label, the app crashes, giving me the error </p>
  
  <pre><code>CoreData: error: Failed to call designated initializer on NSManagedObject class 'NSManagedObject' 
  fatal error: Array index out of range
  </code></pre>
  
  <p>This is my code for the detailed viewController. </p>
  
  <pre><code>@IBOutlet weak var front: UILabel!
  
  var finalArray = [Card1]()
  finalArray = retrievedSet.allObjects as![Card1]
  front.text = finalArray[0].front
  </code></pre>