簡體   English   中英

通過int值過濾具有NSDictionary的NSArray

[英]filter NSArray with NSDictionary by int value

我有以下NSArray:

(
        {
        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是一個整數,其值是:10

但結果我有一個空數組。

希望清楚我的問題,並預先感謝您的回答。

問候,維克多

您的謂詞檢查以查看該值是否等於十,不小於十。 您收到一個空數組,因為您提供給我們的數組不包含establecimiento鍵返回值10的字典。

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

您應該將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

希望這對您最有效和最有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM