[英]Search NSArray of NSDictionary using NSPredicate
所有人,我正在嘗試執行以下操作,但未得到任何結果,也不確定我要去哪里出錯。 在我的情況下, srchString
將從srchString
傳遞,因此搜索字符串將是動態的。 只是出於麻煩,它嘗試對值進行硬編碼,如下例所示,但沒有返回任何結果。 因此,尋求有關如何解決這一問題的幫助。 謝謝
(
{
amount = "-243.77";
date = "May 21, 2014";
description = "Preauthorized Debit";
extraMap = {
MEMO = "ATT Payment";
RUNBAL = "$100,159.51";
id = 123456789454444440000002
};
longDate = "2014-05-22";
referenceId = " ";
sortDate = "2014-05-22 04:00:00 +0000";
transactionDateAsCal = "1432267200000:America-New_York";
},
{
amount = "5058.74";
date = "May 21, 2014";
description = "Deposit";
extraMap = {
MEMO = "WXYZAWQH BATCH";
RUNBAL = "$100,159.51";
id = 124259821201505220000001;
};
longDate = "2014-05-22";
referenceId = " ";
sortDate = "2014-05-22 04:00:00 +0000";
transactionDateAsCal = "1432267200000:America-New_York";
}
)
NSArray *listOfKeys = [[NSArray alloc]initWithObjects:@"amount", @"date", @"description", @"memo", @"runbal", @"referenceId",@"longDate", @"id", nil];
//Sample test. This search string will be passed from the searchDisplayController.
NSString *srchString = @"Preauthorized";
//If the searchstring has an exact match then it works Like below
//NSString *srchString = @"Preauthorized Debit";
NSMutableArray *subpredicates = [NSMutableArray array];
for (NSString *key in listOfKeys) {
NSPredicate *subpredicate = [NSPredicate predicateWithFormat:@"%K contains[cd] %@", key, srchString];
[subpredicates addObject:subpredicate];
}
NSPredicate *resultPredicate = [NSCompoundPredicate orPredicateWithSubpredicates:subpredicates];
NSLog(@"predicate %@",resultPredicate);
NSArray *tempArr = [transactionList filteredArrayUsingPredicate:resultPredicate];
NSLog(@"tempArr %@", tempArr);
好的,因為您對評論的關注度不是很高,所以我會給出答案。 通常情況下,除非我確定OP要求什么,否則我不喜歡這樣做,在這種情況下,我假設我確信自己了解您的願景。 首先,讓我們談談您的示例發生了什么...。並讓我們刪除您共享的“硬編碼”部分,我認為由於語法錯誤和復雜性,這只會使將來的問題尋找者感到困惑。
首先 ,您將%K
用於一個謂詞, %K
是鍵路徑的var arg替代。 在您的實例中,您說您有一個字典數組 ,這最終意味着它是一個數組,不符合KVO,因此不會起作用。
其次 ,您根本不需要循環,實際上,在您的情況下,我不鼓勵這樣做。 僅使用一個謂詞就可以輕松過濾掉它。
因此,讓我們為您和未來的問題尋求者總結一下:
可以說我們有一個如上所述的NSDictionary
的NSArray
。 例如,我們將使用上面提供的OP的數據:
NSArray *firstTransactionKeys = [NSArray arrayWithObjects:@"amount",@"date",@"description",@"longdate", nil];
NSArray *firstTransactionValues = [NSArray arrayWithObjects:@"-243.77",@"May 21, 2014",@"Preauthorized Debit",@"2014-05-22 04:00:00", nil];
NSArray *secondTransactionKeys = [NSArray arrayWithObjects:@"amount",@"date",@"description",@"longdate", nil];
NSArray *secondTransactionValues = [NSArray arrayWithObjects:@"5058.74",@"May 21, 2014",@"Deposit",@"2014-05-22 04:00:00", nil];
NSArray *transactionList = [NSArray new];
transactionList = [NSArray arrayWithObjects:[NSDictionary dictionaryWithObjects:firstTransactionValues forKeys:firstTransactionKeys], [NSDictionary dictionaryWithObjects:secondTransactionValues forKeys:secondTransactionKeys], nil];
NSLog(@"Original Transactions: %@", transactionList);
(
{
amount = "-243.77";
date = "May 21, 2014";
description = "Preauthorized Debit";
longdate = "2014-05-22 04:00:00";
},
{
amount = "5058.74";
date = "May 21, 2014";
description = Deposit;
longdate = "2014-05-22 04:00:00";
}
)
現在我們有了所需的數據,我們所需要做的就是使用代碼友好格式,將數組過濾為關鍵字或搜索詞:
///////Example purposes only. User types in 'Preauthorized Debit'////////
NSString *searchWord = @"Preauthorized Debit";
現在只需使用帶有%@的NSPredicate
(對象參數為var arg)。
NSPredicate *searchPredicate = [NSPredicate predicateWithFormat:@"SELF contains[cd] %@", searchWord];
從謂詞創建新數組:
NSArray *tempArr = [transactionList filteredArrayUsingPredicate:searchPredicate];
NSLog(@"tempArray = %@", tempArr);
就是這樣,希望可以幫助您稍微掌握一些東西
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.