0

I have a basic understanding of predicates, but am stuck with this.

I have 2 Core Data Objects:

Car and Service

Each have a odometer property that is a NSString.

I want to exclude certain results by comparing the odometer readings of both. I want to compare as a Int, not as a NSString.

This will not work, but it should detail what I am trying to do

fetchRequest.predicate = [NSPredicate predicateWithFormat:@"(%i > [serviceOdometer intValue])", [self.currentCar.odometer intValue]];

I have the current Car as an Int, but cannot get the fetched Service odometer as an int.

DogCoffee
  • 19,820
  • 10
  • 87
  • 120
  • If you have an NSString that contains a decimal integer (and on other non-blank characters to confound things) then `intValue` or `integerValue` will return the number. But I don't think you can include a method call in your predicate like that. It might work with dot notation -- `serviceOdometer.intValue`. (Not real familiar with predicates.) – Hot Licks Jan 07 '14 at 01:36
  • yeah, still unable to get it to work. Dot notation fails, this might be a case where i have to use an array rather than a fetched results controller. – DogCoffee Jan 07 '14 at 03:04
  • (On reviewing the predicate documentation I could not find anywhere where parens `()` are mentioned. Are you sure you should have those?) – Hot Licks Jan 07 '14 at 03:26
  • I have used them when grouping multiple statements, i.e. @"(ofCar == %@) AND (completedSer == 0) not sure if it changes anything but makes it a little cleaner to read. I think i will just create a transient property in my data model, and that will return an int of the string. – DogCoffee Jan 07 '14 at 03:30
  • What you suggested should work re: http://stackoverflow.com/questions/8879286/nspredicate-how-to-treat-strings-as-numbers but its still comparing as strings – DogCoffee Jan 07 '14 at 07:08
  • @Smick: The `intValue` in the fetch request is probably simply ignored. Compare http://stackoverflow.com/questions/19197855/using-kvc-in-nssortdescriptor for a similar issue with `integerValue` in a (Core Data) sort descriptor. – Martin R Jan 07 '14 at 13:29
  • @MartinR thanks for that. I want to exclude those results from the fetch so it needed to be in the predicate. And as there is no way to manually remove the items in a fetched results controller looks like I'll need to use an array. If only I could use the block method!! Which don't work for SQL .... any other ideas ?? – DogCoffee Jan 07 '14 at 13:38
  • Can't you change the attribute type to Number? That would be the correct solution. – Martin R Jan 07 '14 at 13:48
  • I'll just stick to using an array for that TableView, but good to know restrictions of core data for future app designs. – DogCoffee Jan 08 '14 at 00:29

0 Answers0