2015-07-20 78 views
3

我尝试过NSPredicate过滤。它不在NSMutableArray中工作,但我尝试在数组中,它工作正常。NSPredicate不适用于在ios中过滤

工作代码中使用数组:

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
  1. 应该是Priceprice,应该是Percentagepercentage
  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; 
} 
) 
+0

对不起,我试图在不同的'p'只是'P',我更新我的问题框架结果数组,但我怎么能实现你的答案 – iOSDev

+0

请注意,在我的代码是@(100),那是NSNumber,而不是@“100” – Leo

+0

感谢它现在的工作,我将@“100”&@“0”更改为@(100)&@(0) – iOSDev