【发布时间】:2015-07-20 13:57:33
【问题描述】:
我已尝试通过 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;
}
)
【问题讨论】:
标签: ios objective-c filter nspredicate