HTML5   发布时间:2022-04-27  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了太多锅炉板代码切换声明 – iOS大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个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所遇到的程序开发问题。

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。