As part of an assignment for a iOS development class, I'm tasked with adding a UISearchBar to a Core Data project written in Swift.
I can't figure out how to get the search bar to compare the text to the objects in my managedObjectContext. I know I'll need to set a predicate and maybe add a few variables. It's the "how" and "where" that's crushing me.
What I THINK I need to do
The managedObjectContext is instantiated in code and there are objects that display in the TVC.
I need to set an NSPredicate
based on searchBar.text
to search the managedObjectContext
and dump the objects in filteredNotes
. This shouldn't be that hard to do, but I find it difficult.
Problems I've encountered:
- Apple's documentation tells me what
UISearchBar
can do, but not how to do it. - Apple's sample code won't compile in Xcode 6.3 (Problem partially solved)
Update: To update sample code from Apple's website to Swift 1.2 when using Xcode 6.3 beta, you can click Edit/Convert
within Xcode to update it. Even though the code compiles without crashing, I still get a console error on Apple's code.
- Existing examples of how to do it are:
- ...written in Objective-C and/or
- ...for filtering static arrays and/or
- ...using deprecated features
My Setup So Far
This post could be a Swift Rosetta Stone for adding a UISearchBar
in a Core Data project's ViewController
. Thank you for reading. If you've got a thought, here are the details of what I've got set up so far.
My Core Data Stack is setup in AppDelegate.swift
using "canned code".
My project is setup as a Master-Detail ViewController
.
My sole NSManagedObject
entity has 4 attributes.
I added UISearchBarDelegate
to the top of my "canned code" MasterVC
:
class MasterViewController: UITableViewController, NSFetchedResultsControllerDelegate, UISearchBarDelegate
// These get set up in the canned "TableView" code, which I've left out
var detailViewController: DetailViewController? = nil
var addNoteViewController:AddNoteViewController? = nil
var managedObjectContext: NSManagedObjectContext? = nil
@IBOutlet weak var searchBar: UISearchBar! // <- Search Stuff
var searchActive : Bool = false // <- Property for UISearchBar delegate methods
var filteredNotes:[Note] = [] // <- Property for Notes (my NSManagedObject)
Below is my section for UISearchBarDelegate optional methods (and the searchBar)
// UISearchBarDelegate methods
func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
searchActive = true;
}
func searchBarTextDidEndEditing(searchBar: UISearchBar) {
searchActive = false;
}
func searchBarCancelButtonClicked(searchBar: UISearchBar) {
searchActive = false;
}
func searchBarSearchButtonClicked(searchBar: UISearchBar) {
searchActive = false;
}
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
filteredNotes = data.filter({ (text) -> Bool in // <- 'data' should be the Note objects in my managed object context
let tmp: NSString = text
let range = tmp.rangeOfString(searchText, options: NSStringCompareOptions.CaseInsensitiveSearch)
return range.location != NSNotFound
})
if(filteredNotes.count == 0){
searchActive = false;
} else {
searchActive = true;
}
self.tableView.reloadData()
}