内容简介:最近在看《重构》的书,想到Xcode有一个Refactor的功能,不知道您用的多不多,用这个功能在我们开发过程中,可以提高开发效率。右键显示重命名符号,修改属性或方法的名字。
最近在看《重构》的书,想到Xcode有一个Refactor的功能,不知道您用的多不多,用这个功能在我们开发过程中,可以提高开发效率。
右键显示
一、Rename
重命名符号,修改属性或方法的名字。 当然有可能您用的是全局Replace这个方法,但是这个无法替换Class的文件名 。 演示下将TestViewController的.h .m .xib及用到的地方修改为有意义的命HomeViewController。
1.在TestViewController上右键点击,选择Refactor->Rename
2.将TestViewController修改为HomeViewController,点击Preview。
3.点击查看并确认,是否是重命名正确。点击Save,再点击Continue。Rename完成。
4.再去全局搜索TestViewController
还有注释中的未被修改,选择Replace替换为HomeViewController。
重命名Property操作也同上。所以看到不符合规范的变量名和方法名及类名,快速的修改吧,提高代码的可读性。
二、 Extract
封装代码。 有时候在键盘上健步如飞的敲写代码,发现一个方法中超过了200行的代码,要进行方法的分割。如提取通用的代码,方法其他方法调用。用Extract很简单。 操作如下:
1.选中需要提取的代码,右键点击,选中Extract
- (void)extracted_method
New function是新函数,C语言的函数。eg.
void extracted_function()
2.修改方法,点击Preview
点击Save,再选择Continue。
3.封装完成
由原来的
- (void)layoutSubviews { [super layoutSubviews]; NSInteger count = [self.subviews count]; for (int i = 0; i < count; i++) { UIButton *btn = self.subviews[i]; btn.tag = i; CGFloat x = i * self.bounds.size.width / count; CGFloat y = 0; CGFloat width = self.bounds.size.width / count; CGFloat height = self.bounds.size.height; btn.frame = CGRectMake(x, y, width, height); } } 复制代码
修改为
- (void)updateButtonFrame { NSInteger count = [self.subviews count]; for (int i = 0; i < count; i++) { UIButton *btn = self.subviews[i]; btn.tag = i; CGFloat x = i * self.bounds.size.width / count; CGFloat y = 0; CGFloat width = self.bounds.size.width / count; CGFloat height = self.bounds.size.height; btn.frame = CGRectMake(x, y, width, height); } } - (void)layoutSubviews { [super layoutSubviews]; [self updateButtonFrame]; } 复制代码
减少很多复制黏贴。
三、 Create Superclass
创建超类。
. 选中类名,点击右键Refactor->Create Superclass
Create files for new superclass: 创建新文件来创建新类 Add superclass to HomeViewController's files: 在HomeViewController中添加新类。
2. 输入超类名称,点击Preview
点击Save,再点击Continue。
3. 修改Superclass的继承类名
完成
四、 Move Up
将实例变量,property变量或方法移动到超类。 移动方法举例 方法申明
- (void)updateUserInfo; 复制代码
方法实现
- (void)updateUserInfo { NSLog(@"Hello World!"); } 复制代码
1. 选中类名, 右键点击Refactor->Move Up
2. 点击Preview
啥都没有显示,点击Save
3. 方法已经到超类BaseViewController
#import <UIKit/UIKit.h> @interface BaseViewController : UIViewController - (void)updateUserInfo; @end 复制代码
五、 Move Down
将实例变量移动到子类。
@interface BaseViewController (){ NSString *schoolNameStr; } 复制代码
1. 选中schoolNameStr,右键Refactor->Move Down
2. 点击Preview, 再点击Save。
在子类HomeViewController中,可以看到schoolNameStr变量。
@interface HomeViewController : BaseViewController { NSString *schoolNameStr; } 复制代码
六、 Encapsulate
创建Setter和Getter方法。 只能对实例变量进行操作,对property无效。
@interface HomeViewController () { NSString *nameStr; } 复制代码
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- RecyclerView使用指南(一)—— 基本使用
- 如何使用Meteorjs使用URL参数
- 使用 defer 还是不使用 defer?
- 使用 Typescript 加强 Vuex 使用体验
- [译] 何时使用 Rust?何时使用 Go?
- UDP协议的正确使用场合(谨慎使用)
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Developer's Guide to Social Programming
Mark D. Hawker / Addison-Wesley Professional / 2010-8-25 / USD 39.99
In The Developer's Guide to Social Programming, Mark Hawker shows developers how to build applications that integrate with the major social networking sites. Unlike competitive books that focus on a s......一起来看看 《Developer's Guide to Social Programming》 这本书的介绍吧!