iphone - 如何将所选 ID 与数组中的国家/地区 ID 进行比较并显示其名称而不是 ID

标签 iphone ios objective-c nsmutablearray nsmutabledictionary

我的 json 响应是这样的。

{
    "continent_code" = EU;
    "country_code" = al;
    "country_id" = 2;
    "country_name" = Albania; 
},
{
    "continent_code" = AF;
    "country_code" = dz;
    "country_id" = 3;
    "country_name" = Algeria;
},

我的代码是这样的。

 country_selected=2; 
 NSMutableArray *wholeJsonArray = [LoginResult objectForKey:@"Response"];
    for(NSDictionary *countname in wholeJsonArray)
    {

        /* 
         NSString *cName = [NSString stringWithFormat:@"%@",[countname     
         objectForKey:@"country_name"]];
         [countryArray addObject:cName];
         */


        NSString *countryName = [countname objectForKey:@"country_name"];
        if(countryName)
            [countryArray addObject:countryName];


        NSString *stateName=[countname objectForKey:@"state_name"];
        if(stateName)
            [stateArray addObject:stateName];
    }

    for(NSDictionary *cid in wholeJsonArray)
    {

        NSNumber *number = [cid objectForKey:@"country_id"];
        if(number)
            [idcountry addObject:number];

        NSNumber *statenumber=[cid objectForKey:@"state_id"];
        if(statenumber)
            [idstate addObject:statenumber];

    }
}

我的问题是,如果用户选择国家 id 2,然后在数组字段中比较这个值,然后在标题标签中显示是阿尔巴尼亚。

那么我该如何做这个输出呢?

请帮帮我。

提前致谢。

最佳答案

country_selected=2; 
NSString *countryName;
for(NSDictionary *countname in wholeJsonArray)
{
    NSNumber *country_id = [countname objectForKey:@"country_id"];
    if(country_selected == country_id.integerValue){
        countryName = [countname objectForKey:@"country_name"];
    }           
}

其中 countryName 将包含选定的 id 国家名称;

关于iphone - 如何将所选 ID 与数组中的国家/地区 ID 进行比较并显示其名称而不是 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16207266/

相关文章:

iphone - 使用 Phonegap 进行 native 应用程序开发

iphone - 是什么决定了 iPhone (mobile safari) 将使用 css 做什么 :hover pseudo-class?

ios - 如何像邮件一样在 UITextView 中显示图像并使其居中

iphone - 如何在运行时将一行插入 iPhone 上的 UITableView?

javascript - 如何在 iPhone 的 Phonegap 应用程序中从 Objective-C 中调用 JavaScript 函数?

ios - 使用 AFNetworking 下载图像 - 使用 ARC 弹出 ViewController 后内存不会释放

iphone - 调用其他类中声明的 IBAction

iphone - 电影项目的 UIImagePickerController

ios - 设置 UI 元素的属性而无需重新启动模拟器

iphone - 如何取消 Twitter 与我的 iOS 应用程序的链接