验证数据并循环访问 NSSet



我在xcode中有一个CoreData(SQLite)数据模型,如下所示:

Friends Table
  email
  name
  username
  belongsToGroups
Groups Table
  title
  peopleInGroup

因此,belongsToGroupspeopleInGroups是彼此之间的多对多关系,两者都由代码中的NSSet表示。

我该使用什么来查询组中人员的NSSet,反之亦然?(我是CoreData的新手)

使用coredata,您可以简单地做到这一点。假设我们在 Group Table 上有一个对象( group),你想得到所有属于 group 的朋友,你可以做到:

[组。 人员在组所有对象]

有关更多详细信息:

  1. 通过标题获取组
NSError* error = nil;
NSFetchRequest* fetchRequest = [[NSFetchRequest alloc] init];
NSPredicate *predicate;
NSEntityDescription *entity;
NSArray *fetchedObjects;    
Group* group;    
entity = [NSEntityDescription
          entityForName:[NSString stringWithFormat:@"%@",[Group class]]
          inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"(title like[c] "%@")" ,title]];
[fetchRequest setPredicate:predicate];
fetchedObjects = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
if (fetchedObjects.count > 0) {
    group = [fetchedObjects lastObject];
}
return group;

}

  1. 获取组的所有好友

    NSMutableArray* friends = [NSMutableArray alloc] init]; [friends addObjectsFromArray:[group. peopleInGroup allObjects]];

最新更新