title: Implementing a UISearchController in Swift Core Data Project
body: <p>I'm presently taking an iOS development course. As part of an assignment, I'm tasked with creating a <code>UISearchController</code> in a note tracking project using Core Data in Swift.</p>
  
  <p>Every example I've found is in Objective-C or is filtering a static array. Apple's "sample" code, updated in December 2014 doesn't compile in Xcode 6.3.</p>
  
  <p>To add a <code>UISearchController</code>, I've got <strong>3 primary tasks to do:</strong></p>
  
  <p>1) Create a view controller to present search results. I'm using a TableViewController.</p>
  
  <p>2) Create a UISearchController, and pass it my search results view controller.</p>
  
  <p><strong>What's "stumping" me is now to get a hold of the objects in the managedObjectsContext.</strong> Prior to attempting to add a <code>UISearchController</code>, my app works fine. I can add, edit, and delete items. I'm using the "canned" Core Data code in Xcode 6.3 with the stack in AppDelegate.</p>
  
  <pre><code>class MasterViewController: UITableViewController, NSFetchedResultsControllerDelegate
  
      var searchController: UISearchController? = nil
  
  func addSearchBar() {
      var resultsController = SearchResultsTableViewController()
      resultsController.notes = // stumped what to call the notes. Trying to call an array from the Notes class below
      resultsController.delegate = self
  
      searchController = UISearchController(searchResultsController: resultsController)
      searchController!.searchResultsUpdater = resultsController
      searchController!.searchBar.frame = CGRect(
          x: searchController!.searchBar.frame.origin.x,
          y: searchController!.searchBar.frame.origin.y, width: searchController!.searchBar.frame.size.width, height: 44.0)
  
      tableView.tableHeaderView = searchController!.searchBar
      self.definesPresentationContext = true
  }
  </code></pre>
  
  <p>3) The UISearchController will notify its searchResultsUpdater (a class that conforms to UISearchResultsUpdating) when the search text changes. I'm using my search results view controller implement this protocol so I can update the filtered results.</p>
  
  <p>Below is my <code>Note: NSManagedObject</code> class:</p>
  
  <pre><code>import Foundation
  import CoreData
  
  class Note: NSManagedObject {
  
  @NSManaged var dateCreated: NSDate
  @NSManaged var dateEdited: NSDate
  @NSManaged var noteTitle: String
  @NSManaged var noteBody: String
  
  // TODO: adding this to make it easier to handle names
  class func notes() -&gt; NSArray {
      let whereMyNotesAreStored = // Need syntax for where my MyManagedObjectContext is located
      let dataArray = NSArray(contentsOfFile: whereMyNotesAreStored!)
  
      var notesArray = NSMutableArray()
  
      for dictionary in dataArray {
          var note = Note()
          note.noteTitle = dictionary["noteTitle"] as! String
          note.noteBody = dictionary["noteBody"] as! String
          note.dateCreated = dictionary["dateCreated"] as! String
          note.dateEdited = dictionary["dateEdited"] as! String
  
          notesArray.addObject(note)
      }
  
      return NSArray(array: notesArray as! [AnyObject])
  }
  
  }
  </code></pre>