我有一個字符串數組,我想用它作爲從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];
}
}
你在哪裏有'詞典'?我有一系列的字典......那麼我該怎麼做呢? –
當你說你有一個字典陣列時,我不確定你的意思,你的plist應該只是一個帶有你在上面提供的字典中的鍵和值的字典。您在上面提供的每個值/密鑰是否在單獨的字典中?如果是這樣,你應該把它們保存在一本字典中,而不是一組字典中。 –
我有一個約30個字典的數組。每個字典都有4個元素。 –