开发者

Extract attributes from NSManagedObject array

开发者 https://www.devze.com 2022-12-28 12:13 出处:网络
NSFetchRequest *req = [NSFetchRequest init]; NSEntityDescription *descr = [NSEntityDescription entityForName:@\"City\" inManagedObjectContext:context];
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 little more intuitive. I am new in it and I could use some help. I fetched all objects(rows) from the entity (table) City. Now I have an array of objects. From the array I need to extract the attribute “streetName” to an array which will feed the picker. I figured I need to do it in the loop but I c开发者_运维技巧ould not figure out the way to do it. Please help.

I have a background with SQL but Core Data is still a big mystery to me. Is there any publication which would take a SQL statement and show comparable Core Data syntax?

Thanks.


It's very simple because of key-value coding:

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


i think core data changed quite a bit. here is how i do my fetch 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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号