NSPredicate problem with column name NSPredicate problem with column name objective-c objective-c

NSPredicate problem with column name


The Predicate Programming Guide from Apple says:

%@ is a var arg substitution for anobject value—often a string, number,or date.

%K is a var arg substitutionfor a key path.

When string variablesare substituted into a format stringusing %@ , they are surrounded byquotation marks. If you want tospecify a dynamic property name, use%K in the format string.

So, in your case, you need to put %K as a keypath to columnName, not %@ which will be added with quotation marks:

NSPredicate *refQuery = [NSPredicate predicateWithFormat: @"%K == %@", columnName, value];

Hope this clear your doubts.


Very weird, but I think I have solved it by doing the following:

NSNumber *value = [NSNumber numberWithInteger: 2];NSString *columnName = @"something_id";NSString *predicate = [NSString stringWithFormat: @"%@ == %@", columnName, value];NSPredicate *refQuery = [NSPredicate predicateWithFormat: predicate];