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 (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to