Extracting Attributes from an NSManagedObject Array

NSFetchRequest *req = [NSFetchRequest init];

NSEntityDescription *descr = [NSEntityDescription entityForName:@"City" inManagedObjectContext:context];
[req setEntity:descr];

NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"streetName" ascending:YES];
[req setSortDescriptors:[NSArray arrayWithObject:sort]];
[sort release];

    //fetch
NSError *error;
NSArray *result = [context executeFetchRequest:req error:&error];

    //extract names
NSMutableArray *streets = [[NSMutableArray alloc] init];

for () {
        ??? = [array objectAtIndex:i];      
        [streets addObject:name];

    }

      

I expected Core Data to be a little more intuitive. I am new to this and I could use some help. I selected all objects (rows) from the entity (table) city. Now I have an array of objects. From the array, I need to extract the "streetName" attribute into an array that will feed the picker. I figured I needed to do it in a loop, but I couldn't figure out how to do it. Please, help.

I have a background with SQL, but Core Data is still a big mystery to me. Is there any post that will take a SQL statement and show comparable Core Data syntax?

Thanks.

+2


a source to share


2 answers


It's very easy because of the keyed coding:



NSArray *streets = [result valueForKey:@"streetName"];

      

+4


a source


I think the kernel data has changed quite a bit. this is how i make my sample now



NSError *error;
NSFetchRequest *fr =[NSFetchRequest fetchRequestWithEntityName:@"Category"];
[fr setPredicate:[NSPredicate predicateWithFormat:@"name == \"myCategpry\""]];
NSArray *rs = [self.managedObjectContext executeFetchRequest:fr error:&error];
for (NSManagedObject *index in rs) {
    NSLog(@"%@",[index primitiveValueForKey:@"name"]);
}

      

0


a source







All Articles