gpt4 book ai didi

ios - [<__NSCFDictionary 0x7fdf8a926250> valueForUndefinedKey :]: this class is not key value coding-compliant for the key HName

转载 作者:行者123 更新时间:2023-12-01 16:32:29 25 4
gpt4 key购买 nike

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

[<__NSCFDictionary 0x7fdf8a926250> valueForUndefinedKey:]: this class is not key value coding-compliant for the key 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";
}

最佳答案

尝试以下格式字符串:

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

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

您必须将通配符添加到 searchHotelNameString如果您使用 like运算符(operator)。

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

关于ios - [<__NSCFDictionary 0x7fdf8a926250> valueForUndefinedKey :]: this class is not key value coding-compliant for the key HName,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31072159/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com