NS字典搜索

NSDictionary Search

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

这是词典的定义

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”和“destination”的输入参数等于“floors[ORIGIN] ”和“楼层[目的地]”。

您的 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];