NSPredicate – filtering values based on a BOOLEAN stored value

This isn’t really specific to NSPredicate… Whenever you have %@ in a format string, the corresponding value must be a pointer to an object, and BOOL doesn’t qualify. So instead of passing YES, pass [NSNumber numberWithBool: YES]. In newer versions of Xcode and the SDKs than when I originally wrote the answer, you can use … Read more

Core Data NSPredicate checking for BOOL value

Based on Apple Document Here, we can use the following two methods to compare Boolean: NSPredicate *newPredicate = [NSPredicate predicateWithFormat:@”anAttribute == %@”,[NSNumber numberWithBool:aBool]]; NSPredicate *testForTrue = [NSPredicate predicateWithFormat:@”anAttribute == YES”]; However, the above predicate cannot get out the ones with empty anAttribute. To deal with an empty attribute, you need the following method according to … Read more

Core Data, NSPredicate and to-many key

To test for an empty relationship you should compare the count of the to-many key to zero. [NSPredicate predicateWithFormat:@”excludedOccurrences.@count == 0″]; As for your subpredicates, be aware that you can only have one of either the ALL or ANY modifiers in your final predicate, although you can use that modifier multiple times throughout the predicate. … Read more

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 … Read more

Core Data primary key ID for a row in the database

-[NSManagedObject objectID] is the unique ID for an object instance in Core Data. It can be serialized via -[NSManagedObjectID URIRepresentation]. You can retrieve the objectID from a persistent store coordinator with -[NSPersistentStoreCoordinator managedObjectIDForURIRepresentation:] and then get the object from a managed object context with -[NSManagedObjectContext objectWithID:]. BUT You should keep in mind that Core Data … Read more

How to search an NSSet or NSArray for an object which has an specific value for an specific property?

NSPredicate *predicate = [NSPredicate predicateWithFormat:@”type == %@”, @”standard”]; NSArray *filteredArray = [myArray filteredArrayUsingPredicate:predicate]; id firstFoundObject = nil; firstFoundObject = filteredArray.count > 0 ? filteredArray.firstObject : nil; NB: The notion of the first found object in an NSSet makes no sense since the order of the objects in a set is undefined.

Combining ‘AND’ and ‘OR’ Condition in NSPredicate

Addition to @Stuart’s answer, you can use NSCompoundPredicate for your OR & AND operations like this. Obj-C – OR // OR Condition // NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@”X == 1″]; NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@”X == 2″]; NSPredicate *predicate = [NSCompoundPredicate orPredicateWithSubpredicates:@[predicate1, predicate2]]; Obj-C – AND NSPredicate *predicate1 = [NSPredicate predicateWithFormat:@”X == 1″]; NSPredicate *predicate2 … Read more

iPhone – getting unique values from NSArray object

Take a look at keypaths. They are super powerful and I use them instead of NSPredicate classes most of the time. Here is how you would use them in your example… NSArray *uniqueStates; uniqueStates = [customObjects valueForKeyPath:@”@distinctUnionOfObjects.state”]; Note the use of valueForKeyPath instead of valueForKey. Here is a more detailed/contrived example… NSDictionary *arnold = [NSDictionary … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)