我正在尝试过滤一系列频道'只找到类型属性为Twitter' Twitter'。但是,我尝试使用NSPredicate过滤数组会返回一个空数组。这是我的代码:
NSMutableDictionary *officials = [decodedData valueForKey:@"officials"];
NSMutableArray *channels = [officials valueForKey:@"channels"];
NSLog(@"%@", channels);
NSPredicate *twitterPredicate = [NSPredicate predicateWithFormat:@"type = 'Twitter'"];
NSLog(@"%@", [channels filteredArrayUsingPredicate:twitterPredicate]);
这是我在应用谓词之前和之后的回报:
在:
(
(
{
id = SenatorFeinstein;
type = Facebook;
},
{
id = SenFeinstein;
type = Twitter;
},
{
id = SenatorFeinstein;
type = YouTube;
}
),
(
{
id = senatorboxer;
type = Facebook;
},
{
id = SenatorBoxer;
type = Twitter;
},
{
id = SenatorBoxer;
type = YouTube;
}
),
)
后:
2015-01-07 10:19:31.760 voices[537:66850] ()
我的谓词过滤器有问题吗?我正在使用适用于iOS的Google Civic API。任何建议表示赞赏
答案 0 :(得分:2)
试试这个,它从通道数组中的数组中得到你所有的dict:
NSMutableDictionary *officials =[decodedData valueForKey:@"officials"];
NSMutableArray *channels = [officials valueForKey:@"channels"];
NSLog(@"%@", channels);
NSMutableArray *onlyChannels = [[NSMutableArray alloc] init];
for (NSArray *array in channels)
{
for (NSDictionary *dict in array)
{
[onlyChannels addObject:dict];
}
}
NSPredicate *twitterPredicate = [NSPredicate predicateWithFormat:@"type = 'Twitter'"];
NSLog(@"%@", [onlyChannels filteredArrayUsingPredicate:twitterPredicate]);