Note that there are some explanatory texts on larger screens.

plurals
  1. PONSPredicate for multiple matches in NSFetchRequest
    primarykey
    data
    text
    <p>First off: sorry if the title isn't very clear, I couldn't quite put my question into short words!</p> <p>Please consider the following scenario:<br> - You are using Core Data for storing objects<br> - You want to fetch objects from your context<br> - You want to include a predicate to only fetch objects with certain properties<br> - You have an NSDictionary containing key-value pairs where the key represents the property name and the value represents the desired value to match against</p> <p><strong>How is best to achieve this?</strong></p> <p>I currently have the following, which is a quick and probably inefficient way of achieving this:</p> <pre><code>NSDictionary *attributes = [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects:@"value1", @"value2", nil] forKeys: [NSArray arrayWithObjects:@"attr1", @"attr2", nil] ]; // Build predicate format NSString *predicate = @""; NSMutableArray *predicateArguments = [[NSMutableArray alloc] init]; int index = 0; for (NSString *key in attributes) { NSString *value = [attributes objectForKey: key]; predicate = [predicate stringByAppendingFormat: @"(%@ = %@) %@", key, @"%@", index == [attributes count]-1 ? @"" : @"AND "]; [predicateArguments addObject: value]; index++; } NSPredicate *matchAttributes = [NSPredicate predicateWithFormat:predicate argumentArray:predicateArguments]; </code></pre> <p>--</p> <p><strong>Is there a shorter or more efficient way of achieving this predicate?</strong></p> <p>Please be aware that block predicates are not an option due to not being supported with NSFetchRequest (Core Data)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload