2 lpploverou LPPloveROU 于 2013.07.23 15:02 提问

访问一对一关系引发了崩溃

有两个表Assets和Rewards,对应关系是一对一。我想访问reward表。

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>)

我想要通过对应关系访问reward内的所有值,或者是一个特定的asset可以得到对应reward的详细内容。

请高手指点。

1个回答

Mr_me
Mr_me   2013.07.23 16:47

你不能使用valueForKey:,在多步骤的路径,比如assetrewards.rewardsId,需要使用valueForKeyPath:。代码在寻找名为assetrewards.rewardsId的关键字,而不是名为assetrewards可以指向rewardsId对象的关键字

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!