coredata - fetch one attribute into an array

前端 未结 2 1119
时光取名叫无心
时光取名叫无心 2021-01-02 03:14

Aim: I would like to fetch the value of one attribute (from an entity) from the database (core data) into an array.

Example

相关标签:
2条回答
  • 2021-01-02 04:00

    You can avoid the last for loop,

    Instead of,

    NSMutableArray *employeeIDs = [NSMutableArray array];
    
    for(id currentRecord in results)
    {
        [employeeIDs addObject:[currentRecord objectForKey:@"employeeID"]];
    }
    

    Try this,

    NSMutableArray *employeeIDs = [results valueForKey:@"employeeID"];
    
    0 讨论(0)
  • 2021-01-02 04:02

    One way of doing it is-

    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Employees"];
    fetchRequest.resultType = NSDictionaryResultType;
    
    NSError *error      = nil;
    NSArray *results    = [self.managedObjectContext executeFetchRequest:fetchRequest
                                                                   error:&error];
    
    NSMutableArray *employeeIDs = [results valueForKey:@"employeeID"];
    
    0 讨论(0)
提交回复
热议问题