gpt4 book ai didi

ios - NSPredicate 不适用于 ios 中的过滤

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:14:26 25 4
gpt4 key购买 nike

我试过通过 NSPredicate 进行过滤。它在 NSMutableArray 中不起作用,但我在 Array 中试过并且它工作正常。

使用数组的工作代码:

filterArray=[NSMutableArray arrayWithObjects:@"Yvan",@"Balu",@"Srinath",@"Aswin",@"Ram", nil];
NSPredicate *bPredicate =[NSPredicate predicateWithFormat:@"SELF beginswith[c] 'r'"];
NSArray *resultAr = [resultArray filteredArrayUsingPredicate:bPredicate];
NSLog(@"Output %@",resultAr);

正确生成:

Output (
Srinath,
Ram
)

我尝试使用包含字典数据的 NSMutableArray,但它不起作用。

构建结果数组是:

for(int i=0;i<[priceArray count];i++)
{
cellDict=[[NSMutableDictionary alloc]init];
NSString *nameStr=nameArray[i];
[cellDict setObject:nameStr forKey:@"Name"];
[cellDict setObject:@([splPriceArray[i] intValue]) forKey:@"Percentage"];
[cellDict setObject:@([priceArray[i] intValue]) forKey:@"Price"];
[resultArray addObject:cellDict];
}

结果数组:

(
{
Name = "Black Eyed Peas";
Percentage = 0;
Price = 80;
},
{
Name = "Black Gram";
Percentage = 0;
Price = 56;
},
{
Name = "Channa White";
Percentage = 0;
Price = 100;
},
{
Name = "Double Beans";
Percentage = 0;
Price = 95;
},
{
Name = "Gram Dall";
Percentage = 0;
Price = 100;
},
{
Name = "Green Moong Dal";
Percentage = 0;
Price = 150;
},
{
Name = "Ground Nut";
Percentage = 0;
Price = 140;
},
{
Name = "Moong Dal";
Percentage = 0;
Price = 75;
},
{
Name = "Orid Dal";
Percentage = 0;
Price = 100;
},
{
Name = "Toor Dal";
Percentage = 0;
Price = 150;
}
)

尝试过的谓词是:

//  NSPredicate *predit=[NSPredicate predicateWithFormat:@"Price contains[c] '100'"];

NSPredicate *pred=[NSPredicate predicateWithFormat:@"(Price == %@) AND (Percentage == %@)", @"100",@"0"];

NSArray *resultAr = [resultArray filteredArrayUsingPredicate:predit];

以上是正确的方法还是有更好的方法来实现它以获得:

expected output:
(
{
Name = "Channa White";
Percentage = 0;
Price = 100;
},
{
Name = "Gram Dall";
Percentage = 0;
Price = 100;
},
{
Name = "Orid Dal";
Percentage = 0;
Price = 100;
}
)

最佳答案

  1. 应该是Price不是price,应该是Percentage不是percentage
  2. 我猜 PercentageNSNumber 的类型

我测试

    NSDictionary * dic1 = @{
@"Name" : @"Black Eyed Peas",
@"Percentage":@(0),
@"Price" : @(80),
};
NSDictionary * dic2 = @{
@"Name" : @"Black Eyed Peas",
@"Percentage":@(0),
@"Price" : @(100),
};
NSMutableArray * mutableArray = [[NSMutableArray alloc] initWithArray:@[dic1,dic2]];
NSPredicate *pred= [NSPredicate predicateWithFormat:@"(Price == %@) AND (Percentage == %@)", @(100),@(0)];

NSArray *resultAr = [mutableArray filteredArrayUsingPredicate:pred];
NSLog(@"%@",resultAr);

日志

2015-07-20 22:11:44.535 OCTest[2192:79846] (
{
Name = "Black Eyed Peas";
Percentage = 0;
Price = 100;
}
)

关于ios - NSPredicate 不适用于 ios 中的过滤,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31518426/

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