2

我有一个字符串数组,我想将其用作从 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];
}
}
4

2 回答 2

0

可能是这样的

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

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

于 2012-12-10T05:27:19.107 回答
0

您可以执行以下操作来按键过滤:

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

或者像这样按值过滤的东西:

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]];
}

更新了数组中字典的答案:

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;
        }
    }
}
于 2012-12-10T06:01:27.507 回答