gpt4 book ai didi

ios - 过滤字典数组时无法解析格式字符串?

转载 作者:行者123 更新时间:2023-11-29 02:37:43 25 4
gpt4 key购买 nike

我有一个字典数组如下

  <__NSArrayI 0x10053e7c0>(
{
busType = "A/C SLEEPER";
fare = 700;
},
{
busType = "Volvo A/C B9R Multi-Axle Semi Sleeper";
fare = 500;
},
{
busType = "2+1 Hi-Tech Non A/c";
fare = 500;
},
{
busType = "Hitech Sleeper Cum Seater";
fare = 1500;
},
{
busType = "Hi-Tech Non A/c";
fare = 900;
},
{
busType = "Mercedes Benz Multi Axle";
fare = 1700;
})

现在我想根据 busType 键的值来过滤这个数组,即 A/C 或 Non A/c 加上 SLEEPER 。所以我将这些值设置为两个 NSString形成一个谓词,其完成如下。

   -(void)applyFilter:(UIButton *)sender{
if (ac_Value.length>1&&sleeper_Value.length>1) {

NSString *dictionaryKey = @"busType";
NSString *predicateString1 = ac_Value; // Set this NSString as A/C or Non A/c
NSString *predicateString2 = sleeper_Value; //Set this NSString as Sleeper or Non/Sleeper
NSPredicate* predicate = [NSPredicate predicateWithFormat:@"%K CONTAINS[cd] %@ %@",dictionaryKey,predicateString1,predicateString2];
NSArray *filteredArray = [busArray filteredArrayUsingPredicate:predicate];
NSLog(@"filtered array : %@", filteredArray);

}


}

但是,我一直在完成谓词形成部分,该部分给出了如下所示的错误。感谢任何帮助。

     *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unable to parse the format string "%K CONTAINS[cd] %@ %@"'

最佳答案

如果我没理解错的话,你可能希望你的谓词看起来像这样:

[NSPredicate predicateWithFormat:@"%K CONTAINS[cd] %@ AND %K CONTAINS[cd] %@",dictionaryKey,predicateString1,dictionaryKey,predicateString2];

或者如果 key 始终是 busType,您可以将其缩短为:

[NSPredicate predicateWithFormat:@"busType CONTAINS[cd] %@ AND busType CONTAINS[cd] %@",predicateString1,predicateString2];

但是,将 ac_Value 设置为“A/C”将匹配同时包含“A/C”和“Non A/c”的值。将 [cd] 更改为 [d] 以创建区分大小写/不区分变音的过滤器。

关于ios - 过滤字典数组时无法解析格式字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26166341/

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