• iOS 简化冗余代码


      正在给深圳某家智能家居开发iPad版本,在已经存在的iPhone版上修改,该app的界面采用的是xib。xib相比代码来写界面,快速高效,但是可维护性和可读性太差。言归正传,看到这些代码后,我的心情很复杂。

      任何一门编程领域,任何人都可以参与,就拿iOS开发来说,有的人写出的代码是能执行,但不仅仅要求是能执行,当项目的功能不断增加时,不整洁的编写风格会让人看着想吐,就不用谈什么可维护性了,简直是一团糟。

     else if (devTypeID==13||devTypeID==14)
        {
            XRDimmingViewController *dimmingVC;
            if (iphone6P)
            {
                dimmingVC = [[XRDimmingViewController alloc]initWithNibName:@"XRDimmingViewController-6P" bundle:nil];
            }
            else if (iphone6)
            {
                dimmingVC = [[XRDimmingViewController alloc]initWithNibName:@"XRDimmingViewController-6" bundle:nil];
            }
            else
            {
                dimmingVC = [[XRDimmingViewController alloc]initWithNibName:@"XRDimmingViewController" bundle:nil];
            }
            dimmingVC.devTypeID = devTypeID;
            dimmingVC.devID = devID;
            [self.navigationController pushViewController:dimmingVC animated:YES];
            return;
        }
        else if (devTypeID==15)
        {
            XRColorViewController *colorVC = [[XRColorViewController alloc] init];
            colorVC.devTypeID = devTypeID;
            colorVC.devID = devID;
            [self.navigationController pushViewController:colorVC animated:YES];
            return;
        }
        else if (devTypeID>=31&&devTypeID<=34)
        {
            XRCurtainViewController *curtainVC;
            if(iphone6)
            {
                curtainVC = [[XRCurtainViewController alloc]initWithNibName:@"XRCurtainViewController-6" bundle:nil];
            }
            else if (iphone6P)
            {
                curtainVC = [[XRCurtainViewController alloc]initWithNibName:@"XRCurtainViewController-6P" bundle:nil];
                
            }
            else
            {
                curtainVC = [[XRCurtainViewController alloc]initWithNibName:@"XRCurtainViewController" bundle:nil];
                
            }
            curtainVC.deviceType = devTypeID;
            curtainVC.curtainDeviceID = devID;
            [self.navigationController pushViewController:curtainVC animated:YES];
            return;
        }
    

       完全可以写一个公用方法判断设备类型来加载对应的xib。

    - (NSString *)selectViewControllerXibName:(NSString *)className {
        NSString *str;
        if (iPhone6p) {
            str = [className stringByAppendingString:@"-6p"];
        } else if (iPhone6) {
            str = [className stringByAppendingString:@"-6"];
        } else {
            str = className;
        }
        return str;
    }
    

       比如下面的这段代码就可以精简了,让程序的逻辑清晰

    // 精简前:
     else if (devTypeID==13||devTypeID==14)
        {
            XRDimmingViewController *dimmingVC;
            if (iphone6P)
            {
                dimmingVC = [[XRDimmingViewController alloc]initWithNibName:@"XRDimmingViewController-6P" bundle:nil];
            }
            else if (iphone6)
            {
                dimmingVC = [[XRDimmingViewController alloc]initWithNibName:@"XRDimmingViewController-6" bundle:nil];
            }
            else
            {
                dimmingVC = [[XRDimmingViewController alloc]initWithNibName:@"XRDimmingViewController" bundle:nil];
            }
            dimmingVC.devTypeID = devTypeID;
            dimmingVC.devID = devID;
            [self.navigationController pushViewController:dimmingVC animated:YES];
            return;
        }
    // 精简后:
    else if (devTypeID==13||devTypeID==14)
    {
        XRDimmingViewController *dimmingVC = [[XRDimmingViewController alloc]initWithNibName:[self selectViewControllerXibName:NSStringFromClass([XRDimmingViewController class])] bundle:nil];
        dimmingVC.devTypeID = devTypeID;
        dimmingVC.devID = devID;
        [self.navigationController pushViewController:dimmingVC animated:YES];
        return;
    }

       

  • 相关阅读:
    Thrift全面介绍
    ZooKeeper全面介绍
    Redis全面介绍
    第三篇:彻底解决ssh.invoke_shell() 返回的中文问题
    第二篇:ssh.invoke_shell() 切换root出现的新问题
    第一篇:UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc3 in position 0: invalid continuation byte
    pycharm安装 suds模块报错:AttributeError: module 'pip' has no attribute 'main'
    变量赋值理解--Pyton中让两个值互换的方法
    02 Django框架基础(APP的创建访问)
    01 Web框架介绍
  • 原文地址:https://www.cnblogs.com/sunminmin/p/4451992.html
Copyright © 2020-2023  润新知