Iphone 多对多关系核心数据的谓词

Iphone 多对多关系核心数据的谓词,iphone,core-data,many-to-many,relationship,nsset,Iphone,Core Data,Many To Many,Relationship,Nsset,在这样的核心数据模型中,我有两个实体:ab 实体B有一个属性名,该属性名是一个字符串对象,并且AObjects与a之间存在关系;相反,实体A已与B建立了关系B对象。 现在我想得到一个与实体连接的所有对象的列表,然后,我想在标签中显示它们的名称 这可能吗?我知道CoreData不支持多对多关系… 谢谢 您可以尝试这样的谓词: NSPredicate*fetchPredicate=[NSPredicate predicateWithFormat:@“任意AOObject=%@”,[A的NSManag

在这样的核心数据模型中,我有两个实体:
ab

实体B有一个属性名,该属性名是一个字符串对象,并且AObjects与a之间存在关系;相反,实体A已与B建立了关系B对象。
现在我想得到一个与实体连接的所有对象的列表,然后,我想在标签中显示它们的名称

这可能吗?我知道CoreData不支持多对多关系…

谢谢

您可以尝试这样的谓词:


NSPredicate*fetchPredicate=[NSPredicate predicateWithFormat:@“任意AOObject=%@”,[A的NSManagedObjectd]]

我认为您可能没有充分描述您的情况,因为当然核心数据绝对支持多对多关系。我怀疑您的意思是NSFetchedResultsController不支持多对多关系?据我所知,这是正确的。(编辑:可以使用具有多对多关系的NSFetchedResultsController…但如何操作并不十分明显。)

要在没有NSFetchedResultsController的情况下执行此操作,请识别/获取您感兴趣的实体,然后遍历您感兴趣的关系。因此,如果您已经知道您对一个特定的对象感兴趣,我将称之为对象,类名为a和B,您可以使用点语法和快速枚举遍历关系,如下所示:

for (B *theBObject in theAObject.BObjects) {
    NSLog(@"theBObject.name: %@.", theBObject.name);
    // Run whatever code you want to here on theBObject.
    // This code will run once for each B Object associated with theAObject 
    // through the BObjects relationship.
}
或者,您可以设置一个获取请求来获取一组您感兴趣的AOObject,然后遍历每个AOObject的BOjects关系。这是一种多对多的关系,没有任何区别。。。每个AOBJECT将返回其BObjects关系中的所有B对象

后来 现在,您说您希望获取所有名称,并将其显示在标签中。让我们为您分析一下:

NSString *myLabel = null;
// You may of course want to be declaring myLabel as a property and @synthesising
// it, but for the sake of a complete example we'll declare it here which means 
// it will only have local scope.

for (B *theBObject in theAObject.BObjects) {
    myLabel = [myLabel stringByAppendingString:theBObject.name];    
    // Add a line break in the string after each B object name.
    myLabel = [myLabel stringByAppendingString:@"\n"];
}

// Do something with your myLabel string to set your desired label.

看一看。非常感谢你。。。你帮我弄明白了这一点:)