【发布时间】:2012-12-19 14:27:36
【问题描述】:
目标:我想从数据库(核心数据)中获取一个属性(来自实体)的值到一个数组中。
示例
实体名称 = 员工
属性 = 员工 ID
我只想将所有的employeeID 填充到一个数组/集合中。
问题
鉴于下面是我的实现,我只是觉得这是一种迂回的方式,我想知道是否有更好的方法来做到这一点。
代码
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Employees"];
fetchRequest.resultType = NSDictionaryResultType;
[fetchRequest setPropertiesToFetch:[NSArray arrayWithObjects:@"employeeID", nil]];
NSError *error = nil;
NSArray *results = [self.managedObjectContext executeFetchRequest:fetchRequest
error:&error];
NSMutableArray *employeeIDs = [NSMutableArray array];
for(id currentRecord in results)
{
[employeeIDs addObject:[currentRecord objectForKey:@"employeeID"]];
}
【问题讨论】:
-
使用 fetch as dictionaries 获取请求选项,并指定“employeeID”作为您要获取的密钥。检查 NSFetchRequest.h。
标签: ios core-data nsfetchrequest