【问题标题】:How to get List from entity in CoreData with all items there is each item appears only once?如何从核心数据中的实体中获取所有项目的列表,每个项目只出现一次?
【发布时间】:2013-09-28 07:11:08
【问题描述】:

在 CoreData 模型中,我有“客户”实体和后续列: customer_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 写这段代码,但还是没有成功。

【问题讨论】:

标签: ios core-data


【解决方案1】:

//将这个字符串写入谓词中

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

【讨论】:

    猜你喜欢
    • 2021-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 2023-02-03
    • 2022-12-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多