0

I have an entity titled Locations, which is initially populated with JSON via Alamofire. On a UIViewController with an embedded TableView and refresh button, I need to search for three specific entries in the Location entity.

Here is what I have so far:

//Refresh button action
    @IBAction func refreshAction(_ sender: AnyObject) {
        let locationPredicate = NSPredicate(format: "title = %@", "Bronco Stadium", "Morrison Center", "2 Morrison Center")
        let fetchRequest:NSFetchRequest<Location> = Location.fetchRequest()
        fetchRequest.predicate = locationPredicate
        do {
            let results = try DatabaseController.getContext().fetch(fetchRequest)
            locationList = results as [Location]
            tableView.reloadData()
        } catch {
            print("Error with request: \(error)")
        }
    }

results is returning with one entry: "Bronco Stadium". My hope is that it would return with the three items passed to the NSPredicate.

I apologize if this is a silly question; I'm new to Swift.

rogerogden
  • 15
  • 4
  • It is fetching with only one attribute because the argument is being passed only once with your `title = %@` so try this: `let locationPredicate = NSPredicate(format: "(title = %@) OR (title = %@) OR (title = %@)", "Bronco Stadium", "Morrison Center", "2 Morrison Center")` – Santosh Oct 26 '16 at 16:25

1 Answers1

0

Right now, You’d want to re-write this as

    let locationPredicate = NSPredicate(format: "title = %@ OR title = %@ OR title = %@", "Bronco Stadium", "Morrison Center", "2 Morrison Center")

or if it stars to get more complex use the technique shown in the answers to most effective way of searching using NSPredicate OR statement

Community
  • 1
  • 1
Matthew Bischoff
  • 1,043
  • 11
  • 27