17

For the use within Core Data I tried to build a NSPredicate object. minLength and maxLength are of typeint:

 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"length >= %@ AND length <= %@",
                          minLength, maxLength];

The program crashes here with an EXC_BAD_ACCESS. This is not the case if I use %d instead of %@:

 NSPredicate *predicate = [NSPredicate predicateWithFormat:@"length >= %d AND length <= %d",
                          minLength, maxLength];

What am I missing here?

Norbert
  • 4,239
  • 7
  • 37
  • 59

2 Answers2

48

%@ is the format specifier for objects. An int is not an object. The format specifier for signed integers is %d or %i.

albertamg
  • 28,492
  • 6
  • 64
  • 71
5

In format for int, you shouldn't use %@, but %i. %@ is for objects`.

MByD
  • 135,866
  • 28
  • 264
  • 277