正在给深圳某家智能家居开发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. 【Qt开发】vs2017+qt5.x编译32位应用

    概述 最近有同学私信我,问如何使用vs2017+qt5.10编译出32位的应用,需要使用msvc2017_x86的插件,然而qt官网并没有提供,只能使用源码编译生成msvc2017_x86插件,使用n ...

  2. android -------- GifImageView 之gif图片加载

    目前支持Gif播放的开源库还是有的,试了一下还是这种好用点,所以来分享下android-gif-drawable是通过JNI来渲染帧的,这种方式比使用WebView或者Movie效率要高 要求Andr ...

  3. CEF 访问需要认证网页或接口(在Request的Headers中添加认证Token)

    转载:https://blog.csdn.net/wdw984/article/details/85275289 1.首先要让我们自己的CefClient这个类公有继承CefRequestHandle ...

  4. 配置mongo.conf文件

    配置mongo.conf文件,优化mongod.exe的启动 在d:/Develop/MongoDB目录下创建mongo.conf文件并写入:   dbpath=d:\Develop\MongoDB\ ...

  5. MSYS2 更新源

    博客转载自:https://blog.csdn.net/puputaoexin/article/details/81780492 在使用msys2下载文件的时候回出现下载速度奇慢,且经常因为各种原因报 ...

  6. Java面试底层原理

    面试发现经常有些重复的面试问题,自己也应该学会记录下来,最好自己能做成笔记,在下一次面的时候说得有条不紊,深入具体,面试官想必也很开心.以下是我个人总结,请参考: HashSet底层原理:(问了大几率 ...

  7. Nginx 配置 HTTPS SSL 代理

    配置文件如下: #user nobody; worker_processes 1; events { worker_connections 1024; } http { include mime.ty ...

  8. LeetCode_443. String Compression

    443. String Compression Easy Given an array of characters, compress it in-place. The length after co ...

  9. [LeetCode] 298. Binary Tree Longest Consecutive Sequence 二叉树最长连续序列

    Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...

  10. LeetCode 611. 有效三角形的个数(Valid Triangle Number)

    611. 有效三角形的个数 611. Valid Triangle Number 题目描述 LeetCode LeetCode LeetCode611. Valid Triangle Number中等 ...