2014-01-09 84 views
0

我有以下的NSArray:過濾NSArray的NSDictionary的與通過int值

(
     { 
     establecimiento = 15; 
     internet = 500; 
    }, 
     { 
     establecimiento = 0; 
     internet = 1024; 
    }, 
     { 
     establecimiento = 24; 
     internet = 300; 
    } 
) 

,我需要的陣列篩選establecimiento < 10

我想這一點:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"establecimiento = %@", [NSNumber numberWithInt:self.establecimientoFiltro]]; 

其中self.establecimientoFiltro是價值的int:10

但我作爲導致一個空數組。

希望能夠清楚地回答我的問題,並提前感謝您的答案。

問候, 維克多

+2

如果你想<10,你爲什麼要用「=」? – rdelmar

+0

@ user3175572查看下面的答案以獲得靈活的輸出結果。 – Pavan

+0

而且沒有必要將您的號碼轉換爲NSNumber - 您可以直接用'%d'使用號碼。 –

回答

1

你謂詞檢查是否值等於十,十年以上。您會收到一個空數組,因爲你提供給我們不含字典,其中establecimiento鍵返回的10

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"establecimiento < 10"]; 
0

值應使用blocksPredicate實現這一目標確實。我們假設你的數組是myArray。現在用你選擇的範圍做一個謂詞。截至目前,你希望它在establecimientoFiltro以內不到10。

NSPredicate *keyPred = [NSPredicate predicateWithBlock:^BOOL(id obj, NSDictionary *bindings) { 
    NSRange myRange = NSMakeRange (0, 9); 
    NSInteger rangeKey = [[obj valueForKey:@"establecimientoFiltro"]integerValue]; 
    if (NSLocationInRange(rangeKey, myRange)) { 
     // found 
     return YES; 
    } else { 
     // not found 
     return NO; 
    } 
}]; 

NSArray *filterArray = [myArray filteredArrayUsingPredicate:keyPred]; 
NSLog(@"%@", filterArray); 

你得到的結果在filteredArray過濾。

希望這是最有效的&對你有幫助。