NSPredicate query for not containing a specific string NSPredicate query for not containing a specific string ios ios

NSPredicate query for not containing a specific string


Your first predicate

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"listingID != %@", sessionID];

should work to find all records where the attribute listingID is not equal to sessionID (provided that listingID and sessionID have the same type).

If both are strings and you want to find all records where listingID does not contain the string sessionID as a substring, then this predicate should work:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT (listingID CONTAINS %@)", sessionID];

Use "CONTAINS[cd]" if the string comparison should be done case and diacritical insensitive.

NOTE: You can specify the attribute name as an argument, but then you must use %K instead of %@ as format:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"NOT (%K CONTAINS %@)", @"listingID", sessionID];