簡體   English   中英

iOS使用數組過濾數組

[英]iOS filter an array with an array

我有一個字符串數組,我希望將其用作從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中的值。

您可以執行以下操作以按鍵進行過濾:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM