我有以下共鸣阵列。请帮我过滤#34; Private"
类型的数组leagues = (
{
id = 23;
member = 1;
name = DelphiFC;
password = "<null>";
type = Public;
username = cyriljose;
},
{
id = 24;
member = 1;
name = FascoBreFC;
password = "<null>";
type = Public;
username = cyriljose;
},
{
id = 25;
member = 1;
name = HingeSpotFC;
password = "<null>";
type = Public;
username = cyriljose;
},
{
id = 22;
member = 1;
name = popilatorFC;
password = 25369852;
type = Private;
username = cyriljose;
},
{
id = 21;
member = 1;
name = trojanFC;
password = 12354687;
type = Private;
username = cyriljose;
}
);
我的意思是我应该得到一个包含联盟名称&#34; Private&#34;的数组。我是否应该使用NSPredicate,如果是,如何?请帮忙
答案 0 :(得分:3)
以下是显示type
为Private
的数据的完整示例。
NSArray *arLeagues = @[@{
@"id":@23,
@"member":@1,
@"name":@"DelphiFC",
@"password":@"<null>",
@"type":@"Public",
@"username":@"cyriljose",
},
@{
@"id":@24,
@"member":@1,
@"name":@"FascoBreFC",
@"password":@"<null>",
@"type":@"Public",
@"username":@"cyriljose",
},
@{
@"id":@25,
@"member":@1,
@"name":@"<null>",
@"password":@"12354687",
@"type":@"Public",
@"username":@"cyriljose",
},
@{
@"id":@22,
@"member":@1,
@"name":@"popilatorFC",
@"password":@"25369852",
@"type":@"Private",
@"username":@"cyriljose",
},
@{
@"id":@21,
@"member":@1,
@"name":@"trojanFC",
@"password":@"12354687",
@"type":@"Private",
@"username":@"cyriljose",
}
];
NSPredicate *predicate = [NSPredicate predicateWithFormat:
@"type contains[cd] %@", @"Private"];
NSArray *matchingDicts = [arLeagues filteredArrayUsingPredicate:predicate];
NSLog(@"%@",matchingDicts);
输出
(
{
id = 22;
member = 1;
name = popilatorFC;
password = 25369852;
type = Private;
username = cyriljose;
},
{
id = 21;
member = 1;
name = trojanFC;
password = 12354687;
type = Private;
username = cyriljose;
}
)
答案 1 :(得分:2)
哦,我的印象是你想要用这些数据解析一个JSON文件。如果是这样,这可能有效:
-(void)parseJSONFile {
NSData *jsonData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"JSON FILE URL HERE..."]];
NSError *error;
NSMutableDictionary *parsedData = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
if ((error == nil) && (parsedData != nil)) {
// Get the data you need.
NSArray *data_id = [[parsedData objectForKey:@"leagues"] valueForKey:@"id"];
NSArray *data_member = [[parsedData objectForKey:@"leagues"] valueForKey:@"member"];
NSArray *data_name = [[parsedData objectForKey:@"leagues"] valueForKey:@"name"];
NSArray *data_password = [[parsedData objectForKey:@"leagues"] valueForKey:@"password"];
NSArray *data_type = [[parsedData objectForKey:@"leagues"] valueForKey:@"type"];
NSArray *data_username = [[parsedData objectForKey:@"leagues"] valueForKey:@"username"];
// Only store the data for items in the array which
// have their "type" property set to "Private".
NSMutableArray *private_data = [[NSMutableArray alloc] init];
for (int loop = 0; loop < [data_type count]; loop++) {
if ([[private_data objectAtIndex:loop] isEqualToString:@"Private"]) {
NSArray *info_block = @[[data_id[loop] description], [data_member[loop] description], [data_name[loop] description], [data_password[loop] description], [data_type[loop] description], [data_username[loop] description]];
[private_data addObject:info_block];
}
}
// Now you have parsed the private only objects.
NSLog(@"%@", private_data);
}
else {
// Error - no data has been downloaded.
NSLog(@"Error: %@", error);
}
}
答案 2 :(得分:1)
尝试使用NSPredicate
NSArray *data = [NSArray arrayWithObject:[NSMutableDictionary dictionaryWithObject:@"Private" forKey:@"type"]];
NSArray *filtered = [data filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(type == %@)", @"Private"]];