我一直在尝试使用NSPredicate来过滤这个数组(它充满了NSDictionaries)...
我的代码数量很少,但是没有用......
以下代码应将label.text更改为AmyBurnett34,但它不会......
NSPredicate *pred = [NSPredicate predicateWithFormat:@"id = %@", [[mightyPlistDict objectForKey:@"pushesArr"] objectAtIndex:indexPath.row]];
NSLog(@"%@",pred);
label.text = [[[twitterInfo filteredArrayUsingPredicate:pred] lastObject] objectForKey:@"screen_name"];
NSLog(@"%@",twitterInfo);
以下是NSLoged的内容......
2012-08-05 11:39:45.929 VideoPush[1711:707] id == "101323790"
2012-08-05 11:39:45.931 VideoPush[1711:707] (
{
id = 101323790;
"screen_name" = AmyBurnett34;
},
{
id = 25073877;
"screen_name" = realDonaldTrump;
},
{
id = 159462573;
"screen_name" = ecomagination;
},
{
id = 285234969;
"screen_name" = "UCB_Properties";
},
{
id = 14315150;
"screen_name" = MichaelHyatt;
}
)
如果您还要NSLog这个......那么阵列是空的......
NSLog(%@,[twitterInfo filteredArrayUsingPredicate:pred]);
答案 0 :(得分:2)
问题是您的谓词正在使用字符串进行比较,而您的内容正在使用数字。试试这个:
NSNumber *idNumber = [NSNumber numberWithLongLong:[[[mightyPlistDict objectForKey:@"pushesArr"] objectAtIndex:indexPath.row] longLongValue]];
NSPredicate *pred = [NSPredicate predicateWithFormat:@"id = %@", idNumber];
答案 1 :(得分:0)
您不确定“id”的值是否为字符串 - 它可能是NSNumber。我建议:
NSUInteger matchIdx = ...;
NSUInteger idx = [array indexOfObjectPassingTest:^BOOL(NSDictionary *dict, NSUInteger idx, BOOL *stop)
{
id obj = [dict objectForKey:@"id"];
// NSLog the class if curious using NSStringFromClass[obj class];
NSUInteger testIdx = [obj integerValue]; // works on strings and numbers
return testIdx == matchIdx;
}
if(idx == NSNotFound) // handle error
NSString *screenName = [[array objectAtIndex:idx] objectForKey:@"screen_name"];
答案 2 :(得分:-1)
NSPredicate用于过滤数组,而非排序数组。
要对数组进行排序,请使用NSArray的sortedArrayUsingDescriptors
方法。
一个例子:
// Define a sort descriptor based on last name.
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"lastName" ascending:YES];
// Sort our array with the descriptor.
NSArray *sortedArray = [originalArray sortedArrayUsingDescriptors:[NSArray arrayWithObjects:descriptor, nil]];