Re: NSPredicate/NSExpression - can they solve this Core Data problem?
Re: NSPredicate/NSExpression - can they solve this Core Data problem?
- Subject: Re: NSPredicate/NSExpression - can they solve this Core Data problem?
- From: Ben Trumbull <email@hidden>
- Date: Fri, 2 Apr 2010 14:42:34 -0700
On Apr 2, 2010, at 1:30 PM, David Hoerl wrote:
>> Having the array of attributes unrolled separately is a little odd. Do you mean you have an array of attribute names from, say the entity, and you want to ask a MO for all its non-nil attribute values and get back an array of matching attribute names for those non-nil values?
>
> Specifically, here is what I'm trying to do:
> I have a NSManagedObject "Address" (and no treecontroller) that has properties - lets say name, homePhone, and workPhone. workPhone is optional.
> So, one Address record has obj.name = "Joe Blow", obj.homePhone = "555-1212" and obj.workPhone = nil
> I put the Address object's property keys in an NSArray *attributes = { @"name", @"homePhone", @"workPhone" )
> The exercise is to reduce the attribute array to just the set of keys that when applied to the object result in a value - that is, not nil.
Two approaches:
NSDictionary* obj = [NSDictionary dictionaryWithObjectsAndKeys:@"Joe Blow", @"name", @"555-1212", @"homePhone", nil];
NSArray* keys = [NSArray arrayWithObjects:@"name", @"homePhone", @"workPhone", nil];
NSPredicate* pred = [NSPredicate predicateWithFormat:@"%@.self != nil", obj];
NSLog(@"filtered array results = %@", [keys filteredArrayUsingPredicate:pred]);
NSComparisonPredicate* exprPred = (NSComparisonPredicate*)[NSPredicate predicateWithFormat:@"SUBQUERY(self, $key, %@.$key != nil) == 0", obj];
NSExpression* expr = [exprPred leftExpression];
NSLog(@"expression subquery results = %@", [expr expressionValueWithObject:keys context:nil]);
- Ben
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden