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.