NSDictionary搜索

时间:2015-04-10 22:55:53

标签: objective-c nsarray nsdictionary

我的NSDictionary只检索词典中的第一个数组。  我不知道它为什么会这样表现。

这是搜索

//Loop through the DataDictionary for ORIGIN and DESTINATION
    for (NSMutableDictionary *floors in [MapData allfloors]){
        //Check if found then set roomListTxt and defaultImage from the Dictionary values
        if ([floors[ORIGIN] isEqualToString:origin] && [floors[DESTINATION] isEqualToString:destination]) {
            searchResults.roomListTxt = floors[DESTINATION_ROOMS];
            searchResults.defaultImage = floors[MAP_IMAGE];
            [self.navigationController pushViewController:searchResults animated:YES];
            //break;
        } else {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Oops!!" message:@"Working to add more routes." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
            [alert show];
            break;
        }
    }

这是Dictionary的定义

NSDictionary *d1Dictionary = @{ORIGIN : @"A1",
                               DESTINATION : @"D1",
                               ORIGIN_ROOMS : @"A101, A102, A113",
                               DESTINATION_ROOMS : @"D100, D100C, D101, D102, D103, D105, D107, D111, D113",
                               MAP_IMAGE : [UIImage imageNamed:@"A1-D1.png"]};
[floorsInformation addObject:d1Dictionary];

NSDictionary *d2Dictionary = @{ORIGIN : @"A1",
                               DESTINATION : @"M1",
                               ORIGIN_ROOMS : @"A101, A102, A113",
                               DESTINATION_ROOMS : @"M104, M105, M106, M107",
                               MAP_IMAGE : [UIImage imageNamed:@"A1-M1.png"]};
[floorsInformation addObject:d2Dictionary];

' 来源的输入参数'和' 目的地'等于" 楼层[ORIGIN] "和" 楼层[目的地] "分别

1 个答案:

答案 0 :(得分:0)

您的else块在循环内。它只显示第一个结果,因为在第二个循环中,您的条件评估为NO,触发警报和break命令。

您可以通过设置断点并单步执行代码来验证这一点。您可以根据应用程序的功能,在迭代循环之前或之后检查相应的数据来解决此问题。

例如:

for (NSMutableDictionary *floors in [MapData allfloors]){
    //Check if found then set roomListTxt and defaultImage from the Dictionary values
    if ([floors[ORIGIN] isEqualToString:origin] && [floors[DESTINATION] isEqualToString:destination]) {
        searchResults.roomListTxt = floors[DESTINATION_ROOMS];
        searchResults.defaultImage = floors[MAP_IMAGE];
        [self.navigationController pushViewController:searchResults animated:YES];
        return;
    }
}


// due to the 'return' statement, this code won't get called if a match is made
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Oops!!" message:@"Working to add more routes." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];