简体   繁体   English

iOS使用数组过滤数组

[英]iOS filter an array with an array

I have an array of strings that I want to use as the filter for another array of dictionaries that is created from a plist. 我有一个字符串数组,我希望将其用作从plist创建的另一个词典数组的过滤器。 For example, if I had a plist of dictionaries that looked like so: 例如,如果我有一个像这样的字典列表:

Key:      Value:
car1      audi
car2      bmw
car3      bmw
car4      audi
car5      jaguar

and my array of strings was "audi, jaguar". 我的字符串数组是“ audi,jaguar”。 How would I code it so that I can create a new array that would return "car1, car4, car5"? 我将如何编码它以便创建可以返回“ car1,car4,car5”的新数组? Hope this makes sense. 希望这是有道理的。 Or better yet, how can I walk down this dictionary and filter it based on a value and then create a new array of dictionaries to use. 或者更好的是,我该如何走下这本字典并根据一个值对其进行过滤,然后创建一个新的字典数组来使用。

Code: 码:

-(void)plotStationAnnotations {

desiredDepartments = [[NSMutableArray alloc] init];

BOOL tvfrSwitchStatus = [[NSUserDefaults standardUserDefaults] boolForKey:@"tvfrSwitchStatus"];
BOOL hfdSwitchStatus =  [[NSUserDefaults standardUserDefaults] boolForKey:@"hfdSwitchStatus"];

if (tvfrSwitchStatus) {
    NSString *tvfr = @"TVF&R";
    [desiredDepartments addObject:tvfr];
}
if (hfdSwitchStatus) {
    NSString *hfd = @"HFD";
    [desiredDepartments addObject:hfd];
}

NSLog(@"Array 1 = %@", desiredDepartments);

NSString *path = [[NSBundle mainBundle] pathForResource:@"stationAnnotations" ofType:@"plist"];
NSMutableArray *anns = [[NSMutableArray alloc] initWithContentsOfFile:path];

NSMutableArray *newDictionaryArray = [NSMutableArray array];
for (NSDictionary *dictionary in anns) {
    for (NSString *string in desiredDepartments) {
        if ([dictionary allKeysForObject:string]) {
            [newDictionaryArray addObject:dictionary];
            break;
        }
    }
}

NSLog(@"Array = %@", keyMutableArray);

for (int i = 0; i < [keyMutableArray count]; i++) {

    float realLatitude = [[[keyMutableArray objectAtIndex:i] objectForKey:@"latitude"] floatValue];
    float realLongitude = [[[keyMutableArray objectAtIndex:i] objectForKey:@"longitude"] floatValue];

    StationAnnotations *myAnnotation = [[StationAnnotations alloc] init];
    CLLocationCoordinate2D theCoordinate;
    theCoordinate.latitude = realLatitude;
    theCoordinate.longitude = realLongitude;
    myAnnotation.coordinate = theCoordinate;
    myAnnotation.title = [[keyMutableArray objectAtIndex:i] objectForKey:@"station"];
    myAnnotation.subtitle = [[keyMutableArray objectAtIndex:i] objectForKey:@"department"];
    [mapView addAnnotation:myAnnotation];
}
}

May be something like 可能像

NSArray *array1;
if([array1 containsObject : someValue])

can help. 能够帮助。 someValue can be your values you want to check if they exist in array1. someValue可以是您要检查它们是否存在于array1中的值。

You can do something like this to filter by keys: 您可以执行以下操作以按键进行过滤:

NSArray *keysToLookFor = [NSArray arrayWithObjects:@"car1", @"car4", @"car5", nil];
NSArray *foundObjects = [dictionary objectsForKeys:keysToLookFor notFoundMarker:nil];

Or something like this to filter by values: 或类似这样的东西来过滤值:

NSString *valueToLookFor = @"Audi";
NSArray *keyArray = [dictionary allKeysForObject:valueToLookFor];

// To filter by multiple values
NSArray *valuesToFilterBy = [NSArray arrayWithObjects:@"Bmw", @"Audi", nil];
NSMutableArray *keyMutableArray = [NSMutableArray array];
for (NSString *string in valuesToFilterBy) {
    [keyMutableArray addObjectsFromArray:[dictionary allKeysForObject:string]];
}

Updated answer for dictionaries in arrays: 更新了数组中字典的答案:

NSArray *dictionaryArray; // The array of dictionaries that you have
NSMutableArray *newDictionaryArray = [NSMutableArray array];
NSArray *valuesToFilterBy = [NSArray arrayWithObjects:@"Bmw", @"Audi", nil];
for (NSDictionary *dictionary in dictionaryArray) {
    for (NSString *string in valuesToFilterBy) {
        if ([dictionary allKeysForObject:string]) {
            [newDictionaryArray addObject:dictionary];
            break;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM