Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EXC_BAD_ACCESS when trying to build string by using %@ for an int

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?

like image 385
Norbert Avatar asked Jul 24 '11 12:07

Norbert


2 Answers

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

like image 89
albertamg Avatar answered Nov 05 '22 00:11

albertamg


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

like image 5
MByD Avatar answered Nov 05 '22 01:11

MByD