Consider the following:
NSFetchRequest *request = [[NSFetchRequest Alloc] init];
request.entity = [NSEntityDescription entityWithName:@"Person" inContext:_MOC];
request.propertiesToFetch = [NSArray arrayWithObject:@"Name"];
NSError *error = nil;
NSArray *results = [_MOC executeFetchRequest:request error:&error];
This returns an array of Person objects. What I want is an array of Person.name values from those objects. Currently I walk the results array, extract the names and build a new array. Is there a cleaner, faster way to do this? I’ve thought about changing request.resultType to NSDictionaryResultType but that doesn’t buy much as I still need to transform the array of dictionary into the array I need.
I already have the solution above implemented, so really looking for a better way. If the correct answer is “there is no better way” that’s fine, just making sure I’m not missing something. Thanks!
EDIT: while thinking about this, I’m questioning my need for an array of values vs. just using the array of managed objects. In any case, would still appreciate a great answer if there’s one out there.
Ask for
NSDictionaryResultType, and then with the resulting array of dictionaries, just ask for[array valueForKey:@"name"]. When anNSArrayreceives-valueForKey:it returns a newNSArraycreated from the results of calling-valueForKey:on all its elements.