2013-02-07 117 views
4

我正在开发iOS应用程序和CoreData。实体Shop不是密钥值编码兼容密钥“category.name”

我有这两个实体:

enter image description here

类别

enter image description here

我试图从访问category.nameShop实体,而是我得到一个错误:

- (void)updateDetails:(NSManagedObject *)shop 
{ 
    NSLog(@"updateDetails: %@", shop); 

    if (shop == nil) 
     return; 

    self.nameLabel.text =  [[shop valueForKey:@"name"] description]; 
    self.categoryLabel.text = [[shop valueForKey:@"category.name"] description]; 
    self.addressLabel.text = [[shop valueForKey:@"address"] description]; 
    self.telephoneLabel.text = [[shop valueForKey:@"telephone"] description]; 

    NSNumberFormatter* f = [[NSNumberFormatter alloc] init]; 
    [f setNumberStyle:NSNumberFormatterDecimalStyle]; 
    NSNumber* acceptRate = [f numberFromString:[[shop valueForKey:@"acceptRate"] description]]; 

    _ratingControl.rating = [acceptRate unsignedIntValue]; 
} 

我找回Shop实体是这样的:

NSManagedObjectContext *context = [self managedObjectContext]; 
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Shop" 
              inManagedObjectContext:context]; 

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
[fetchRequest setEntity:entity]; 

NSArray *results = [context executeFetchRequest:fetchRequest error:nil]; 

但我得到这个错误:

'[<NSManagedObject 0x1cdb4890> valueForUndefinedKey:]: the entity Shop is not key value coding-compliant for the key "category.name".'

我怎样才能解决这个问题错误?

回答

7

self.categoryLabel.text = [[shop valueForKey:@"category.name"] description];

应该是

self.categoryLabel.text = [[shop valueForKeyPath:@"category.name"] description];

原因:从Key Value Coding Documentation

的关键是识别对象的特定属性的字符串。通常,键对应于接收对象中的访问器方法或实例变量的名称。密钥必须使用ASCII编码,以小写字母开头,并且不得包含空格。

一些示例密钥将是payee,openingBalance,transactionsamount

关键路径是一串点分隔的键,用于指定要遍历的对象属性序列。序列中第一个键的属性是相对于接收者的,并且每个后续键都相对于前一个属性的值进行评估。

例如,关键路径address.street将从接收对象获取地址属性的值,然后确定相对于地址对象的街道属性。

+2

刚刚添加 - 这来自[Key-Value Coding](https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/KeyValueCoding/Articles/KeyValueCoding.html) – Abizern

+0

@Abizern谢谢!我已经通过添加的链接更新了我的答案。 – Gudiya

相关问题