3

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()
}
Adrian
  • 16,233
  • 18
  • 112
  • 180
  • Have you looked at [this Apple sample code in Swift](https://developer.apple.com/library/ios/samplecode/TableSearch_UISearchController/Introduction/Intro.html)? – Aaron Brager Feb 23 '15 at 16:24
  • That was the second place I went after reading the documentation. In Xcode 6.3, it won't compile. Tons of errors related to `as`. When I switch them to `as!`, clean and build, it generates more errors and it turns into a snowball of fail. – Adrian Feb 23 '15 at 16:30
  • Here's a [link to the project](https://github.com/AdrianBinDC/blocnotes/tree/add-pull-down-search-bar-take-4) – Adrian Feb 23 '15 at 16:40
  • The sample code won't get updated until Xcode 6.3 is out of beta. I would stick to the released versions (currently Xcode 6.1.1) while you're learning. – Aaron Brager Feb 23 '15 at 19:13
  • 2
    But if you want to use Xcode 6.3, you could try Edit - Convert - To Swift 1.2… to migrate the code. – Aaron Brager Feb 23 '15 at 19:14
  • I had no idea you could convert the version of Swift within Xcode! THAT will make it a lot easier to look through examples, follow definitions, etc. I wish I'd known this a few days ago. Better late than never ;) Thank you for the tip. – Adrian Feb 23 '15 at 19:31

0 Answers0