太多锅炉板代码切换声明 – iOS

太多锅炉板代码切换声明 – iOS,第1张

概述我有一个switch语句,它将适当地添加11个UILabel的文本.但是,它现在变得有点长,而且相同代码的副本太多了.我该如何进一步改进它?我可以制作一个标签数组,然后通过for循环访问它们吗? 这是代码切换语句: switch ([number_sorted count]) { case 1: label_1.text = [NSStri 我有一个switch语句,它将适当地添加11个UILabel的文本.但是,它现在变得有点长,而且相同代码的副本太多了.我该如何进一步改进它?我可以制作一个标签数组,然后通过for循环访问它们吗?

这是代码切换语句:

switch ([number_sorted count]) {            case 1:                label_1.text = [Nsstring stringWithFormat:@"%ld",(long)[number_sorted[0] integerValue]];                break;            case 2:                label_1.text = [Nsstring stringWithFormat:@"%ld",(long)[number_sorted[0] integerValue]];                label_2.text = [Nsstring stringWithFormat:@"%ld",(long)[number_sorted[1] integerValue]];                break;            case 3:                label_1.text = [Nsstring stringWithFormat:@"%ld",(long)[number_sorted[1] integerValue]];                label_3.text = [Nsstring stringWithFormat:@"%ld",(long)[number_sorted[2] integerValue]];                break;            case 4:                label_1.text = [Nsstring stringWithFormat:@"%ld",(long)[number_sorted[2] integerValue]];                label_4.text = [Nsstring stringWithFormat:@"%ld",(long)[number_sorted[3] integerValue]];                break;            case 5:                label_1.text = [Nsstring stringWithFormat:@"%ld",(long)[number_sorted[3] integerValue]];                label_5.text = [Nsstring stringWithFormat:@"%ld",(long)[number_sorted[4] integerValue]];                break;            case 6:                label_1.text = [Nsstring stringWithFormat:@"%ld",(long)[number_sorted[4] integerValue]];                label_6.text = [Nsstring stringWithFormat:@"%ld",(long)[number_sorted[5] integerValue]];                break;            case 7:                label_1.text = [Nsstring stringWithFormat:@"%ld",(long)[number_sorted[5] integerValue]];                label_7.text = [Nsstring stringWithFormat:@"%ld",(long)[number_sorted[6] integerValue]];                break;            case 8:                label_1.text = [Nsstring stringWithFormat:@"%ld",(long)[number_sorted[6] integerValue]];                label_8.text = [Nsstring stringWithFormat:@"%ld",(long)[number_sorted[7] integerValue]];                break;            case 9:                label_1.text = [Nsstring stringWithFormat:@"%ld",(long)[number_sorted[7] integerValue]];                label_9.text = [Nsstring stringWithFormat:@"%ld",(long)[number_sorted[8] integerValue]];                break;            case 10:                label_1.text = [Nsstring stringWithFormat:@"%ld",(long)[number_sorted[8] integerValue]];                label_10.text = [Nsstring stringWithFormat:@"%ld",(long)[number_sorted[9] integerValue]];                break;            case 11:                label_1.text = [Nsstring stringWithFormat:@"%ld",(long)[number_sorted[9] integerValue]];                label_11.text = [Nsstring stringWithFormat:@"%ld",(long)[number_sorted[10] integerValue]];                break;            default:                break;        }

谢谢你的时间,丹.

解决方法 另一种选择是使用switch语句的“fall through”属性

switch ([number_sorted count]) {    case 11:        label_11.text = [Nsstring stringWithFormat:@"%ld",(long)[number_sorted[10] integerValue]];    case 10:        label_10.text = [Nsstring stringWithFormat:@"%ld",(long)[number_sorted[9] integerValue]];    case 9:        label_9.text = [Nsstring stringWithFormat:@"%ld",(long)[number_sorted[8] integerValue]];    case 8:        label_8.text = [Nsstring stringWithFormat:@"%ld",(long)[number_sorted[7] integerValue]];    case 7:        label_7.text = [Nsstring stringWithFormat:@"%ld",(long)[number_sorted[6] integerValue]];    case 6:        label_6.text = [Nsstring stringWithFormat:@"%ld",(long)[number_sorted[5] integerValue]];    case 5:        label_5.text = [Nsstring stringWithFormat:@"%ld",(long)[number_sorted[4] integerValue]];    case 4:        label_4.text = [Nsstring stringWithFormat:@"%ld",(long)[number_sorted[3] integerValue]];    case 3:        label_3.text = [Nsstring stringWithFormat:@"%ld",(long)[number_sorted[2] integerValue]];    case 2:        label_2.text = [Nsstring stringWithFormat:@"%ld",(long)[number_sorted[1] integerValue]];    case 1:        label_1.text = [Nsstring stringWithFormat:@"%ld",(long)[number_sorted[0] integerValue]];    default:        break;}
总结

以上是内存溢出为你收集整理的太多锅炉板代码切换声明 – iOS全部内容,希望文章能够帮你解决太多锅炉板代码切换声明 – iOS所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1076955.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存