2012-12-10 45 views
2

我有一個字符串數組,我想用它作爲從plist創建的另一個字典數組的過濾器。舉例來說,如果我有字典的plist中,看起來像這樣:iOS使用數組過濾數組

Key:  Value: 
car1  audi 
car2  bmw 
car3  bmw 
car4  audi 
car5  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]; 
} 
} 

回答

0

可能類似於

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

可以提供幫助。 someValue可以是你想要檢查它們是否存在於array1中的值。

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

你在哪裏有'詞典'?我有一系列的字典......那麼我該怎麼做呢? –

+0

當你說你有一個字典陣列時,我不確定你的意思,你的plist應該只是一個帶有你在上面提供的字典中的鍵和值的字典。您在上面提供的每個值/密鑰是否在單獨的字典中?如果是這樣,你應該把它們保存在一本字典中,而不是一組字典中。 –

+0

我有一個約30個字典的數組。每個字典都有4個元素。 –