1

我有 2 个表 Assets 和 Rewards 有相关的 Assets<---->Rewards,关系名称为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>)

我想访问具有关系/或特定资产的奖励字段的所有值,因为有奖励相关我想获得奖励的详细信息

4

1 回答 1

1

您不能valueForKey:在像assetrewards.rewardsId. 你必须使用valueForKeyPath:. 正如所写的那样,代码正在寻找一个名为 的键assetrewards.rewardsId而不是一个名为 的键assetrewards,它指向一个具有名为 的键的对象rewardsId

于 2013-07-22T16:09:48.617 回答