Core data 如何基于关系筛选CoreData结果

Core data 如何基于关系筛选CoreData结果,core-data,nspredicate,Core Data,Nspredicate,我有一个用户列表,每个用户都有 用户.accounts-->accounts.measurements-->measurement.account 我想从测量“表”中提取所有测量值,其中measurement.account=一个特定的帐户,其中measurement.type(属性)是“风速”,作为示例 因此,我: 在度量值上创建获取请求 我想创建一个谓词,表示type=“wind speed”和account={NSAccount object} 我该怎么做 测量类型很简单: *predic

我有一个用户列表,每个用户都有

用户.accounts-->accounts.measurements-->measurement.account

我想从测量“表”中提取所有测量值,其中measurement.account=一个特定的帐户,其中measurement.type(属性)是“风速”,作为示例

因此,我: 在度量值上创建获取请求 我想创建一个谓词,表示type=“wind speed”和account={NSAccount object}

我该怎么做

测量类型很简单:

*predicate = [NSPredicate predicateWithFormat:@"measuretype = 'air speed'];
指定关系,我不知道:

myAccountObject *myAccount=[accountFromId:@"6785"];
*accountpredicate = [NSPredicate predicateWithFormat:@"account = ???",myAccount];

在谓词中,
%@
是对对象值的var arg替换,因此

[NSPredicate predicateWithFormat:@"account = %@",myAccount]
可能就是你要找的。即使对于常量字符串,这也是可取的,因为它避免了嵌入引号或其他特殊字符的问题,这将 必须以其他方式引用。所以你的组合谓词是:

[NSPredicate predicateWithFormat:@"measuretype = %@ AND account = %@",
        @"air speed", myAccount]
有关详细信息,请参见中的
“谓词编程指南”。

您是否尝试创建谓词?那是什么?它做了什么?添加到上述代码中。