2015-07-20 77 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