如何从Core Data中的实体获取所有项目的列表,每个项目只出现一次?

时间:2013-09-28 07:11:09

标签: ios core-data

在CoreData模型中,我有“customers”实体,其中包含以下列: custostomer_id,姓名,城市,电话

其中包含以下数据:

1                Mary        Los-Angeles     054-112233
2                John        New-York        054-334455
3                Anna        Los-Angeles     054-445566

如何获得每个城市仅出现一次的所有客户城市的列表。对于上面的例子,它应该是{Los-Angeles,New-Yoirk}。

这是我的代码:

  AppDelegate * delegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];
  NSManagedObjectContext *context = delegate.managedObjectContext ;
  NSEntityDescription *entityDescN = [NSEntityDescription entityForName:@"customers" inManagedObjectContext:context];
  NSFetchRequest *requestN = [[NSFetchRequest alloc] init];
  [requestN setEntity:entityDescN ];
  NSPredicate *predN = [NSPredicate predicateWithFormat:@"?????????????"];
  [requestN setPredicate:predN];
  NSArray * objectsN = [context executeFetchRequest: requestN error:&error];

我的意思是应该是什么而不是“???”在NSPredicate定义? 我试图用NSExpression编写这段代码,但仍然没有成功。

1 个答案:

答案 0 :(得分:0)

//在谓词中写这个字符串

NSPredicate *predN = [NSPredicate predicateWithFormat:@"SELECT city FROM customers GROUP BY  city"];