替换目标c中的switch语句 [英] substitute to switch statement in objective c

查看:92
本文介绍了替换目标c中的switch语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用异步连接中的json框架从Web服务加载一个tableview。数据采用json对象形式

I am loading a tableview with the contents from web service using json frameworks in asynchronous connection. The data is in the json object form

 {"id":1,"firstName":"A","lastName":"B","email":"abc@yahoo.com","salary":    {"monthly":$5000,"annual":$60000}}

我在cellForRowAtIndexPath中使用switch语句加载tableview:

I am loading tableview using switch statement in cellForRowAtIndexPath:

 dictionaryData = [responseString JSONValue];
switch (indexPath.row)
{


case 0:
    cell.textLabel.text = [NSString stringWithFormat:@"%@ : %@ %@",@"Name",[dictionaryData valueForKey:@"firstName"],[dictionaryData valueForKey:@"lastName"]];
    break;

    case 1:
        cell.textLabel.text = [NSString stringWithFormat:@"%@ : %@",@"Email",[dictionaryData valueForKey:@"email"]];
        break;

    case 2:
        cell.textLabel.text = [NSString stringWithFormat:@"%@ : %@",@"Monthly Salary",[[dictionaryData valueForKey:@"salary"]valueForKey:@"monthly"]];;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        break;

    case 3:
        cell.textLabel.text = [NSString stringWithFormat:@"%@ : %@",@"Annual Salary",[[dictionaryData valueForKey:@"salary"]valueForKey:@"annual"]];
        break;

    default:
        break;
}

这是正常数据,但当我有更多字段,如电话号码,地址,部门编号等,然后写太多的情况会使方法非常大。有人可以帮助我如何在没有切换的情况下做到这一点。

This is for normal data, but when i have more fields like phone number, address, department number, etc , then writing too many cases will make the method very large.Can someone help me how i can do this without switch.

推荐答案

你这是错误的方式。您希望使用indexpath.row创建数组并对其进行索引。所以你只有一行分配cell.textLabel.text。一个解决方案是:
事先创建一个NSArray对象,包含你的@Name,@E-Mail等,如:

You are going about this the wrong way. You want to create arrays and index them using indexpath.row. So you ll have only one line of assigning cell.textLabel.text. One solution is: Create an NSArray of objects beforehand containing your @"Name",@"E-Mail" etc. like:

NSArray *arr1=[NSArray arrayWithObjects:@"Name",@"Email",....,nil];

然后当您获得NSDictionary时,将其存储在一个数组中并通过它进行枚举,如

Then when you get the NSDictionary,store it in an array and enumerate through it like

NSMutableArray *arr2=[dictionaryData allValues];
for(id obj in arr2)
{
    if([obj isKindOfClass:[NSString class]])
        [arr2 addObject:obj];
    else if([obj isKindOfClass:[NSDictionary class]])
    {
        [arr2 addObject:[obj valueForKey:@"Monthly"]];
        [arr2 addObject:[obj valueForKey:@"Annual"]];
    }
}

然后在cell.textLabel中使用indexpath.row。 text

Then just use indexpath.row in cell.textLabel.text

cell.textLabel.text=[NSString stringWithFormat:@"%@ : %@",[arr1 objectAtIndex:indexPath.row],[arr2 objectAtIndex:indexPath.row]];

当然,可能有更好的方法针对您的情况,但这应该有助于您构建它。

Of course there might be a better way specific to your case, but this should help you build that.

这篇关于替换目标c中的switch语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆