正在给深圳某家智能家居开发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;
}

  

iOS 简化冗余代码的更多相关文章

  1. iOS开发通过代码方式使用AutoLayout (NSLayoutConstraint + Masonry)

    iOS开发通过代码方式使用AutoLayout (NSLayoutConstraint + Masonry) 随着iPhone6/6+设备的上市,如何让手头上的APP适配多种机型多种屏幕尺寸变得尤为迫 ...

  2. iOS项目冗余资源扫描脚本

    iOS项目冗余资源扫描脚本 随着iOS项目的版本不断迭代,app中冗余文件会越来越多,app size也持续增加,是时候需要对app冗余资源进行检测,对app进行瘦身. 使用方法: 1. 运行环境为m ...

  3. Async/Await是这样简化JavaScript代码的

    译者按: 在Async/Await替代Promise的6个理由中,我们比较了两种不同的异步编程方法:Async/Await和Promise,这篇博客将通过示例代码介绍Async/Await是如何简化J ...

  4. lombok 简化java代码注解

    lombok 简化java代码注解 安装lombok插件 以intellij ide为例 File-->Setting-->Plugins-->搜索"lombok plug ...

  5. Lombok简化Java代码

    导包:import lombok.Data; Lombok简化Java代码: 在Lombok中,生成构造方法的annotation一共有三个:@NoArgsConstructor, @Required ...

  6. 基于MVC4+EasyUI的Web开发框架经验总结(11)--使用Bundles处理简化页面代码

    在Web开发的时候,我们很多时候,需要引用很多CSS文件.JS文件,随着使用更多的插件或者独立样式文件,可能我们的Web界面代码会越来越臃肿,看起来也很累赘,在MVC里面提供了一个Bundle的对象, ...

  7. 批量去除Teleport Pro整站下载文件冗余代码

    teleport pro tppabs标签批量删除 teleport pro tppabs标签批量删除 使 用Teleport Pro下载的网页代码中包含了很多垃圾代码,比如下载的html网页代码中会 ...

  8. 去掉tppabs冗余代码,怎样批量去掉tppabs代码

    去掉tppabs冗余代码,怎样批量去掉tppabs代码 刚用teleport pro拉了一个整站到本地 所有的超链都被强行加了一句tppabs="   就玩了一把dw的替换功能 查找范围:整 ...

  9. Teleport Ultra/Teleport Pro的冗余代码批量清理方法

    Teleport Pro 是款优秀的网站离线浏览工具(即网站整站下载工具),Teleport Ultra是其增强版,但使用此系列软件下载的离线网页里会包含大量冗余代码(如tppabs),手动去修改工作 ...

随机推荐

  1. 使用MSCK命令修复Hive表分区

    set hive.strict.checks.large.query=false; set hive.mapred.mode=nostrict; MSCK REPAIR TABLE 表名; 通常是通过 ...

  2. PostgreSQL中的Object Identifier(oid)数据类型

    PostgreSQL在内部使用对象标识符(OID)作为各种系统表的主键.OID不会添加到用户创建的表中,除非在创建表时指定了WITH OIDS,或者启用了default_with_oids配置变量.类 ...

  3. 运行okvis-mono

    ./build/okvis_app_synchronous config/config_fpga_p2_euroc1.yaml ../mav0

  4. Python - Django - form 组件常用的字段和字段参数

    邮箱: views.py: from django import forms from django.forms import widgets class RegForm(forms.Form): e ...

  5. LeetCode:接雨水【42】

    LeetCode:接雨水[42] 题目描述 给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水. 上面是由数组 [0,1,0,2,1,0,1,3,2,1, ...

  6. TP5 模型CURD

    ThinkPHP5的模型是一种对象-关系映射(Object / Relation Mapping ,简称 ORM)的封装,并且提供了简洁的ActiveRecord实现.一般来说,每个数据表会和一个“模 ...

  7. [转帖]说一说JVM双亲委派机制与Tomcat

    说一说JVM双亲委派机制与Tomcat https://www.cnblogs.com/dengchengchao/p/11844022.html 讲个故事: 以前,爱捣鼓的小明突然灵机一动,写出了下 ...

  8. spring整合quartz报错

    今天spring整合quartz报错,最后一步步排查,发现是和redis依赖冲突,最后redis升级了一下,问题解决. 总结:发现问题,逐一排查,如果是整合问题,报类加载不到的错误,大概率是和其他组件 ...

  9. java email发送(附件中文的处理)

    这里使用的是commons-email-1.3.2.jar进行的开发,自认为这是简单的邮件发送. package com.yt.base.common; import java.io.Unsuppor ...

  10. laravel中一些非常常用的php artisan命令

    php artisan 命令在开发laravel项目中非常常用,下面是一些总结 composer config -g repo.packagist composer https://mirrors.a ...