title: Retrieve Random String from Core Data in Swift
body: <p>I've searched for this answer but have come up empty handed. For some reason I'm drawing a blank on how to retrieve a random entry from a Core Data String attribute. I have everything else working - create, update, delete... but can't seem to get a handle on getting a random entry from the stack.</p>
  
  <p>Feeling confused on this. Here is my code from my table view controller that mostly manages the core data.</p>
  
  <pre><code>import UIKit
  import CoreData
  
  class TaskManagerViewController: UITableViewController, NSFetchedResultsControllerDelegate {
  
      let managedObjectContext = (UIApplication.sharedApplication().delegate as AppDelegate).managedObjectContext
  
      var fetchedResultController: NSFetchedResultsController = NSFetchedResultsController()
  
      override func viewDidLoad() {
          super.viewDidLoad()
  
          fetchedResultController = getFetchedResultController()
          fetchedResultController.delegate = self
          fetchedResultController.performFetch(nil)
      }
  
      override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject!) {
          if segue.identifier == "edit" {
              let cell = sender as UITableViewCell
              let indexPath = tableView.indexPathForCell(cell)
              let taskController:TaskDetailViewController = segue.destinationViewController as TaskDetailViewController
              let task:Tasks = fetchedResultController.objectAtIndexPath(indexPath!) as Tasks
              taskController.task = task
          }
      }
  
  
      func getFetchedResultController() -&gt; NSFetchedResultsController {
          fetchedResultController = NSFetchedResultsController(fetchRequest: taskFetchRequest(), managedObjectContext: managedObjectContext!, sectionNameKeyPath: nil, cacheName: nil)
          return fetchedResultController
      }
  
      func taskFetchRequest() -&gt; NSFetchRequest {
          let fetchRequest = NSFetchRequest(entityName: "Tasks")
          let sortDescriptor = NSSortDescriptor(key: "desc", ascending: true)
          fetchRequest.sortDescriptors = [sortDescriptor]
          return fetchRequest
      }
  
      override func didReceiveMemoryWarning() {
          super.didReceiveMemoryWarning()
          // Dispose of any resources that can be recreated.
      }
  
      // #pragma mark - Table view data source
  
      override func numberOfSectionsInTableView(tableView: UITableView) -&gt; Int {
          let numberOfSections = fetchedResultController.sections?.count
          return numberOfSections!
      }
  
      override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
          let numberOfRowsInSection = fetchedResultController.sections?[section].numberOfObjects
          return numberOfRowsInSection!
      }
  
      override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -&gt; UITableViewCell {
              var cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as UITableViewCell
              let task = fetchedResultController.objectAtIndexPath(indexPath) as Tasks
              cell.textLabel.text = task.desc
              return cell
      }
  
  
      override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
          let managedObject:NSManagedObject = fetchedResultController.objectAtIndexPath(indexPath) as NSManagedObject
          managedObjectContext?.deleteObject(managedObject)
          managedObjectContext?.save(nil)
      }
  
      func controllerDidChangeContent(controller: NSFetchedResultsController!) {
          tableView.reloadData()
      }
  }
  </code></pre>