views:

127

answers:

1

I'm trying to create an array of objects from a hierarchy of other objects like this:

code 1:

childController.names = [[NSMutableArray alloc] init];

for (Person *p in list.persons) {
    [childController.names addObject:p.name];
}

code 2:

NSMutableArray *testArray = [list.persons valueForKey:@"name"];

The first code snippet works perfectly, returning an array of persons' names. In contrast, the second returns an array with the correct number of persons' names, but they are shown as "Out of scope" when I debug the code in XCode. When I'm trying to access one of them, the app terminates with:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFSet objectAtIndex:]: unrecognized selector sent to instance 0x72e0620'

The list, persons are Core Data managed objects if it matters.. What am I doing wrong???

+1  A: 

You should check the type of persons. It seems like it is a NSSet, and valueForKey: on NSSet will return a NSSet. This code snippet should work as expected:

NSSet* testSet = [list.persons valueForKey:@"name"];
PeyloW
Absolutely correct!
johnl