ios - iOS使用数组过滤数组

标签 ios nsarray plist nspredicate

我有一个字符串数组,我希望将其用作从plist创建的另一个词典数组的过滤器。例如,如果我有一个像这样的字典列表:

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

我的字符串数组是“audi,jaguar”。我将如何编码它以便创建可以返回“car1,car4,car5”的新数组?希望这是有道理的。或者更好的是,我该如何走下这本字典并根据一个值对其进行过滤,然后创建一个新的字典数组来使用。

码:
-(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];
}
}

最佳答案

可能像

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

可以帮助。 someValue可以是您要检查它们是否存在于array1中的值。

关于ios - iOS使用数组过滤数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13795413/

相关文章:

java - 查询 Firebase 的位置范围

IOS Google+ 分享显示空的 prefillText

iphone - 特定索引后升序

IOS - Objective-C ,如何读取核心数据并将其存储到可变数组中

ios - 为自定义文本编辑器实现辅助功能

ios - 如何创建一个 "array of selectors"

ios - 使用 NSPredicate 在自定义对象的 NSArray 中搜索字符串

objective-c - 将 PList 中的数据获取到 UITableView 中?

ios - 使类符合 NSCoding

iphone - Plist 未从字典保存(保存到文档)