【问题标题】:[<__NSCFDictionary 0x7fdf8a926250> valueForUndefinedKey:]: this class is not key value coding-compliant for the key HName[<__NSCFDictionary 0x7fdf8a926250> valueForUndefinedKey:]:这个类对于键 HName 不是键值编码兼容的
【发布时间】:2015-09-13 08:20:19
【问题描述】:

大家好,我有一个字典数组,我想从中查找酒店名称,但不断收到此错误

[<__nscfdictionary> valueForUndefinedKey:]:这个类 与键 HName 的键值编码不兼容。

我的代码是:

searchHotelNameString = _txtHotelSearch.text;
_resultObjectsArray = [NSArray array];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"@HName like %@",
                                      searchHotelNameString];
_resultObjectsArray = [self.arrHotelResults filteredArrayUsingPredicate:predicate];

我的字典是:

{
    "@HIndex" = 5;
    "@HName" = "XYZ Plaza Hotel Dubai";
    "@Lattitude" = "25.2174";
}

【问题讨论】:

  • 问题是你的 @ 前缀(它们是谓词中的特殊字符,所以如果你查看错误,@ 已从谓词中删除,属性名称变为 @987654325 @ 当然不存在)。如果这不能改变(你的前缀),像@"self['@HName'] like %@" 这样的东西就可以了。

标签: ios objective-c nsdictionary


【解决方案1】:

尝试以下格式字符串:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF['@HName'] CONTAINS %@",
                              searchHotelNameString];

或者

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF['@HName'] LIKE %@",
                              [[@"*" stringByAppendingString:searchHotelNameString] stringByAppendingString:@"*"]];

如果您使用like 运算符,则必须将通配符添加到searchHotelNameString

https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/Predicates/Articles/pCreating.html


编辑:

如果你不使用正则表达式,我建议使用== 而不是LIKE

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF['@HName'] == %@",
                              searchHotelNameString];

这是我的测试代码:

NSString *searchHotelNameString = @"Taj Palace";
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF['@HName'] == %@",
                          searchHotelNameString];

NSArray *arrHotelResults = @[
                             @{@"@HIndex" : @5, @"@HName" : @"XYZ Plaza Hotel Dubai", @"@Lattitude" : @25.2174},
                             @{@"@HIndex" : @5, @"@HName" : @"Beijing", @"@Lattitude" : @25.2174},
                             @{@"@HIndex" : @5, @"@HName" : @"Xiamen", @"@Lattitude" : @25.2174},
                             @{@"@HIndex" : @5, @"@HName" : @"Wuhan", @"@Lattitude" : @25.2174},
                             @{@"@HIndex" : @5, @"@HName" : @"Taj Palace", @"@Lattitude" : @25.2174}
                             ];
NSArray *resultObjectsArray = [arrHotelResults filteredArrayUsingPredicate:predicate];
NSLog(@"%@", resultObjectsArray);

【讨论】:

  • 感谢它的工作,但我必须搜索相同的名称,我的意思是如果我必须搜索 Taj Palace,那么搜索字符串应该是 Taj Palace,如果我输入 taj Palace 它没有显示结果。
  • 你可以用==代替LIKE
  • 我已经发布了我的测试代码,你可以和你的代码对比一下。
  • 您的代码运行良好,但它是区分大小写的搜索 taj Palace 而不是 Taj Palace,您不会得到任何结果
  • 知道了。你应该使用SELF['@HName'] LIKE[cd] %@LIKE[cd] 是修改后的 LIKE 运算符,不区分大小写和变音符号。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-08-07
  • 1970-01-01
  • 2015-05-08
  • 2016-11-13
  • 1970-01-01
  • 2015-06-23
  • 1970-01-01
相关资源
最近更新 更多