我正在做一个项目,其中我正在从异步连接加载带有 JSON 数据的 TableView 。我正在使用 switch
语句加载每一行,如下所示:
dictionaryData = [responseString JSONValue];
switch (indexPath.row)
{
case 0:
{
NSString *name = [NSString stringWithFormat:@"%@ : %@ %@",@"Name",[dictionaryData valueForKey:@"firstName"],[dictionaryData valueForKey:@"lastName"]];
cell.textLabel.text = name;
break;
}
case 1:
{
NSString *email = [NSString stringWithFormat:@"%@ : %@",@"Email",[dictionaryData valueForKey:@"email"]];
cell.textLabel.text = email; }
break;
有 8 行,我必须编写 8 个 switch case,我认为这使我的方法太长了。
谁能告诉我是否有任何替代 switch 语句的方法。
最佳答案
用阵列替换您的开关。数组中的每个索引将对应于前一个 case
s。应该使用您需要的键设置数组
keyArray = [NSArray arrayWithObjects:@"Name", @"Email", ..., nil];
dictionaryData = [responseString JSONValue];
NSString * rowKey = [keyArray objectAtIndex:[indexPath row]];
[[cell textLabel] setText:[dictionaryData objectForKey:rowKey]];
关于objective-c - 替代 Objective-C 中的 switch 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8060572/