如何从 CoreData 中的实体获取列表,其中包含所有项目,每个项目只出现一次



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

它包含以下数据:

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

如何获取每个城市仅出现一次的所有客户城市的列表。对于上面的例子,它应该是{洛杉矶,新约尔克}。

这是我的代码:

  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编写这段代码,但仍然没有成功。

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

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

最新更新