Xcode有一個(gè)Refactor的功能,不知道你用的多不多,但這個(gè)功能在我們開發(fā)過程中,著實(shí)可以提高開發(fā)效率。

右鍵顯示

一、Rename
重命名符號(hào),修改屬性或方法的名字。
當(dāng)然有可能您用的是全局Replace這個(gè)方法,但是這個(gè)無法替換Class的文件名。
演示下將TestViewController的.h .m .xib及用到的地方修改為有意義的命名HomeViewController。
1.在TestViewController上右鍵點(diǎn)擊,選擇Refactor->Rename

2.將TestViewController修改為HomeViewController,點(diǎn)擊Preview。

3.點(diǎn)擊查看并確認(rèn),是否是重命名正確。點(diǎn)擊Save,再點(diǎn)擊Continue。Rename完成。
4.再去全局搜索TestViewController,還有注釋中的未被修改,選擇Replace替換為HomeViewController。

重命名Property操作也同上。所以看到不符合規(guī)范的變量名和方法名及類名,快速的修改吧,提高代碼的可讀性。
二、 Extract
封裝代碼。
有時(shí)候在鍵盤上健步如飛的敲寫代碼,發(fā)現(xiàn)一個(gè)方法中超過了200行的代碼,要進(jìn)行方法的分割。如提取通用的代碼,方法其他方法調(diào)用。用Extract很簡單。
操作如下:
1.選中需要提取的代碼,右鍵點(diǎn)擊,選中Extract

New method 是新方法,OC的方法。eg. - (void)extracted_method
New function是新函數(shù),C語言的函數(shù)。eg. void extracted_function()
2.修改方法,點(diǎn)擊Preview,點(diǎn)擊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];
}
減少很多復(fù)制黏貼。
三、 Create Superclass
創(chuàng)建超類。
1. 選中類名,點(diǎn)擊右鍵Refactor->Create Superclass

Create files for new superclass: 創(chuàng)建新文件來創(chuàng)建新類
Add superclass to HomeViewController's files: 在HomeViewController中添加新類。
2. 輸入超類名稱,點(diǎn)擊Preview,點(diǎn)擊Save,再點(diǎn)擊Continue。

3. 修改Superclass的繼承類名,完成!
四、 Move Up
將實(shí)例變量,property變量或方法移動(dòng)到超類。
移動(dòng)方法舉例
方法申明
- (void)updateUserInfo;
方法實(shí)現(xiàn)
- (void)updateUserInfo
{
NSLog(@"Hello World!");
}
1. 選中類名, 右鍵點(diǎn)擊Refactor->Move Up
2. 點(diǎn)擊Preview,啥都沒有顯示,點(diǎn)擊Save。
3. 方法已經(jīng)到超類BaseViewController
#import <UIKit/UIKit.h>
@interface BaseViewController : UIViewController
- (void)updateUserInfo;
@end
五、 Move Down
將實(shí)例變量移動(dòng)到子類。
@interface BaseViewController (){
NSString *schoolNameStr;
}
1. 選中schoolNameStr,右鍵Refactor->Move Down
2. 點(diǎn)擊Preview, 再點(diǎn)擊Save。
在子類HomeViewController中,可以看到schoolNameStr變量。
@interface HomeViewController : BaseViewController {
NSString *schoolNameStr;
}
六、 Encapsulate
創(chuàng)建Setter和Getter方法。
只能對(duì)實(shí)例變量進(jìn)行操作,對(duì)property無效。
@interface HomeViewController ()
{
NSString *nameStr;
}
1. 選中nameStr,點(diǎn)擊右鍵Refactor->Encapsulate

2. 修改Getter和Setter的方法名后,點(diǎn)擊Preview

3. 點(diǎn)擊Save
- (NSString *)nameStr {
return nameStr;
}
- (void)setNameStr:(NSString *)newValue {
nameStr = newValue;
}
Setter和Getter的方法生成完成。