我有2个表资产和奖励有关联 资产< ---->奖励,当我尝试访问奖励表字段时,使用relationshipname assetrewards
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Assets"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"assetsId == %@",str];
[request setPredicate:predicate];
[self.managedObjectContext executeFetchRequest:request onSuccess:^(NSArray *assetsArr) {
for (Assets *assets in assetsArr) {
NSLog(@"assets %@",[assets valueForKey:@"assetrewards.rewardsId"]);
}
} onFailure:^(NSError *error) {
NSLog(@"Error fetching: %@", error);
}];
然后我收到一份崩溃报告
: Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<Assets 0x9fb9380> valueForUndefinedKey:]: the entity Assets is not key value coding-compliant for the key "assetrewards.rewardsId".'
但是当我像智慧一样检查时
NSLog(@"assets %@",[assets valueForKey:@"assetrewards"]);
我得到这样的输出:
assets <Rewards: 0xad8a4f0> (entity: Rewards; id: 0x9fe4bf0 <x-coredata://B2D33481-2AF2-4C6D-9428-33BDCF653881-15578-00004A2B32ACC29E/Rewards/pc05848de529d4cbab6eb4475a5f9f591> ; data: <fault>)
我希望通过关系/或特定资产访问奖励字段的所有值,因为有奖励相关我想获得奖励的详细信息
答案 0 :(得分:1)
您无法在valueForKey:
等多步键路径上使用assetrewards.rewardsId
。您必须使用valueForKeyPath:
。如上所述,代码正在寻找一个名为assetrewards.rewardsId
,而不是的密钥,该密钥名为assetrewards
,该密钥通向一个名为rewardsId
的密钥的对象。