替代目标C中的switch语句 [英] Alternative to switch statement in objective C

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

问题描述

我正在做一个项目,其中我从异步连接的JSON数据加载表视图。我使用开关语句来加载每一行,如下所示:

  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语句。

解决方案

在某些情况下,一个对象 b

更新



我详细说明了如何以这种方式在代码中替换switch语句


I am doing a project in which I am loading a table view with JSON data from an asynchronous connection. I am using a switch statement to load each row, like so:

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;

There are 8 rows and i have to write 8 switch cases , which i think makes my method too long. Can anyone tell me that is there any alternative to switch statements.

解决方案

In some cases an object would be a better alternative.

Update

I detailed how one might approach this (in an overengineered manner) here: Replace switch statement in the code

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

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