0

I'm using Core Data to store an object model consisting of several entities, but the relevant one is this:

Entity: Stretch

Relevant Attributes:
equipmentUsed, muscleGroup

I have a scene in storyboard with a UISegmentedControl I use to choose to select equipmentUsed.

// Creates a variable which changes the selectedEquipment type based on which 
// selectedSegmentIndex is clicked

    var stretches: [Stretch] = [] // List of stretches
    var selectedEquipment : NSString? // Equipment selected from UISegmentedControl

    @IBAction func selectEquipment(sender: UISegmentedControl) {
        if equipmentSelector.selectedSegmentIndex == 0 {
            self.selectedEquipment = "roller"
        }
        if equipmentSelector.selectedSegmentIndex == 1 {
            self.selectedEquipment = "none"
        }
    }

Below the UISegmentedControl, I have static cells that I use to toggle various muscle groups. Every example of filtering using predicates dumps all the data on a TableView, then filters afterwards. I'm trying to pick the key paths for a predicate and put them in an array.

I'm trying to create predicates that select a particular piece of equipment (ex: None or Foam Roller) AND a particular muscle group. I'm using the selectedEquipment string from earlier as a variable in the predicate below:

var shoulderPredicate = NSPredicate(format: "stretch.equipmentUsed contains[c] $selectedEquipment AND (stretch.muscleGroup contains[c] %@", "shoulders")

I'm running into trouble creating an array from shoulderPredicate. Here's my code for creating array:

@IBAction func testSearchStretches(sender: AnyObject) {
    println("testSearchStretches clicked")
    var stretchList = (stretches as NSArray).filteredArrayUsingPredicate(shouldersPredicate)
    for stretch in stretchList {
        println(stretch)
    }
}

I can see the testSearchStretches IBAction prints "testStretches clicked" in the console, but it doesn't print stretchList, but I know there's a Stretch that matches the predicate. I believe the predicate I set up is case-insensitive, so I don't think that's the problem.

Once I get the array figured out, I'm going to display the results on another screen. For now, I'm just trying to get the predicate working to create the stretchList array. I'm new to programming and I'm trying to move beyond tutorials to create something on my own. If you made to here, thanks for reading and I greatly appreciate your help.

iOSPadawan
  • 146
  • 2
  • 10
  • You could just use `let shoulderPredicate = NSPredicate { (evaluatedObject, _) in return __your_condition_checks_here__ }`. I appreciate that they tried to come up with a "human-readable" predicate syntax, but sometimes just using a block is easier (it's always more powerful). – Ian MacDonald Jan 13 '15 at 20:08

1 Answers1

1

There are two problems with your predicate:

  • You must not include the entity name in the key paths.
  • $selectedEquipment in a predicate format string does not insert or interpolate the value of the selectedEquipment variable. $VAR in a predicate can be used with the predicateWithSubstitutionVariables() function, but I assume that is not what you want here.

Your predicate should probably be

NSPredicate(format: "equipmentUsed contains[c] %@ AND muscleGroup contains[c] %@",
     selectedEquipment, "shoulders")

assuming that "equipmentUsed" and "muscleGroup" are String attributes.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • Yes, _equipmentUsed_ and _muscleGroup_ are both strings. I tried that originally and I get this compiler error: **'StretchSelectorViewController.Type' does not have a member named 'selectedEquipment'** – iOSPadawan Jan 13 '15 at 20:07
  • 1
    @iOSPadawan: OK, that is a different problem. The initialization value of the property `shoulderPredicate` cannot depend on the values of other properties. You should move the creation of the predicate into some method. See (for example) http://stackoverflow.com/questions/25855137/viewcontrol-type-does-not-have-a-member-named or http://stackoverflow.com/questions/25854300/change-the-x-and-y-in-a-cgrectmake for a similar issues. – Martin R Jan 13 '15 at 20:09
  • Thank you! I guess it's back to the drawing board on that part. Thank you for pointing me in the right direction. I greatly appreciate it. – iOSPadawan Jan 13 '15 at 20:37