根据coredata中的属性名称获取唯一对象

时间:2015-06-10 11:51:07

标签: ios objective-c iphone core-data

我在coredata

中有实体employee_detail
name | rate | factor |
_______|______|________|
John   |  3.2 |    4   |
Betty  |  5.5 |    7   |
Betty  |  2.1 |    2   |
Betty  |  3.1 |    2   |
Edward |  4.5 |    5   |
John   |  2.3 |    4   |

我想要基于属性名称的唯一对象

O / P应该是

name | rate | factor |
_______|______|________|
John   |  3.2 |    4   |
Betty  |  5.5 |    7   |
Edward |  4.5 |    5   |

1 个答案:

答案 0 :(得分:2)

NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"employee_detail"];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"employee_detail" inManagedObjectContext:self.managedObjectContext];

fetchRequest.resultType = NSDictionaryResultType;
fetchRequest.propertiesToFetch = [NSArray arrayWithObject:[[entity propertiesByName] objectForKey:@"name"]];
fetchRequest.returnsDistinctResults = YES;

NSArray *dictionaries = [self.managedObjectContext executeFetchRequest:fetchRequest error:nil];
NSLog (@"names: %@",dictionaries);