成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

如何利用Objective-C寫一個精美的DSL

移動開發(fā) iOS
在程序開發(fā)中,我們總是希望能夠更加簡潔、更加語義化地去表達(dá)自己的邏輯,鏈?zhǔn)秸{(diào)用是一種常見的處理方式。我們常用的 Masonry、 Expecta 等第三方庫就采用了這種處理方式。這種鏈?zhǔn)秸{(diào)用能夠使程序更加清晰,在特定場景下使程序的可讀性更強(qiáng)。這種手段在Swift也是相同道理,大家可以善加利用,讓自己的代碼更加美觀。

iOS

推薦序:本文是來自美團(tuán)的 iOS 技術(shù)專家臧成威的投稿。臧老師在 StuQ 開完 RactiveCocoa 的兩次系列課程后,最近新開了一門 《iOS 實(shí)戰(zhàn)黑魔法》的新課程,課程內(nèi)容涉及很多 Objective-C Runtime, Swift 等底層的知識和應(yīng)用技巧,如果你感興趣,可以看文末的介紹。

感謝臧成威的授權(quán),以下是文章正文。

背景

在程序開發(fā)中,我們總是希望能夠更加簡潔、更加語義化地去表達(dá)自己的邏輯,鏈?zhǔn)秸{(diào)用是一種常見的處理方式。我們常用的 Masonry、 Expecta 等第三方庫就采用了這種處理方式。

  1. // Masonry 
  2. [view1 mas_makeConstraints:^(MASConstraintMaker *make) { 
  3.     make.top.equalTo(superview.mas_top).with.offset(padding.top); 
  4.     make.left.equalTo(superview.mas_left).with.offset(padding.left); 
  5.     make.bottom.equalTo(superview.mas_bottom).with.offset(-padding.bottom); 
  6.     make.right.equalTo(superview.mas_right).with.offset(-padding.right); 
  7. }]; 

 

 

  1. // Expecta 
  2. expect(@"foo").to.equal(@"foo"); // `tois a syntactic sugar and can be safely omitted. 
  3. expect(foo).notTo.equal(1); 
  4. expect([bar isBar]).to.equal(YES); 
  5. expect(baz).to.equal(3.14159); 

 

像這種用于特定領(lǐng)域的表達(dá)方式,我們叫做 DSL (Domain Specific Language),本文就介紹一下如何實(shí)現(xiàn)一個鏈?zhǔn)秸{(diào)用的 DSL.

鏈?zhǔn)秸{(diào)用的實(shí)現(xiàn)

我們舉一個具體的例子,比如我們用鏈?zhǔn)奖磉_(dá)式來創(chuàng)建一個 UIView,設(shè)置其 frame、backgroundColor, 并添加至某個父 View。

對于最基本的 Objective-C (在 iOS4 block 出現(xiàn)之前),如果要實(shí)現(xiàn)鏈?zhǔn)秸{(diào)用,只能是這個樣子的:

  1. UIView *aView = [[[[UIView alloc] initWithFrame:aFrame] bgColor:aColor] intoView:aSuperView]; 

有了 block,我們可以把中括號的這種寫法改為點(diǎn)語法的形式

  1. UIView *aView = AllocA(UIView).with.position(x, y).size(width, height).bgColor(aColor).intoView(aSuperView); 
  2.  
  3. // 當(dāng)x和y為默認(rèn)值0和0或者width和height為默認(rèn)值0的時候,還可以省略 
  4. UIView *bView = AllocA(UIView).with.size(width, height).bgColor(aColor).intoView(aSuperView); 

 

可以看出,鏈?zhǔn)秸Z法的語義性很明確,后者的語法更加緊湊,下面我們從兩個角度看一下后者的實(shí)現(xiàn)。

1. 從語法層面來看

鏈?zhǔn)秸{(diào)用可以用兩種方式來實(shí)現(xiàn):

1).在返回值中使用屬性來保存方法中的信息

比如,Masonry 中的 .left .right .top .bottom 等方法,調(diào)用時會返回一個 MASConstraintMaker 類的實(shí)例,里面有 left/right/top/bottom 等屬性來保存每次調(diào)用時的信息;

  1. make.left.equalTo(superview.mas_left).with.offset(15); 

再比如,Expecta 中的方法 .notTo 方法會返回一個 EXPExpect 類的實(shí)例,里面有個 BOOL 屬性 self.negative 來記錄是否調(diào)用了 .notTo;

  1. expect(foo).notTo.equal(1); 

再比如,上例中的 .with 方法,我們可以直接 return self;

2).使用 block 類型的屬性來接受參數(shù)

比如 Masonry 中的 .offset(15) 方法,接收一個 CGFloat 作為參數(shù),可以在 MASConstraintMaker 類中添加一個 block 類型的屬性:

  1. @property (nonatomic, copy) MASConstraintMaker* (^offset)(CGFloat); 

比如例子中的 .position(x, y),可以給的某類中添加一個屬性:

  1. @property (nonatomic, copy) ViewMaker* (^position)(CGFloat x, CGFloat y); 

在調(diào)用 .position(x, y) 方法時,執(zhí)行這個block,返回 ViewMaker 的實(shí)例保證鏈?zhǔn)秸{(diào)用得以進(jìn)行。

2. 從語義層面來看

從語義層面上,需要界定哪些是助詞,哪些是需要接受參數(shù)的。為了保證鏈?zhǔn)秸{(diào)用能夠完成,需要考慮傳入什么,返回什么。

還是以上面的例子來講:

  1. UIView *aView = AllocA(UIView).with.position(x, y).size(width, height).bgColor(aColor).intoView(aSuperView); 

分步來看一下,這個 DSL 表達(dá)式需要描述的是一個祈使句,以 Alloc 開始,以 intoView 截止。在 intoView 終結(jié)語之前,我們對 UIView 進(jìn)行一定的修飾,利用 position size bgColor 這些。

下面我們分別從四段來看,如何實(shí)現(xiàn)這樣一個表達(dá)式:

(1) 賓語

在 AllocA(UIView) 的語義中,我們確定了賓語是 a UIVIew。由于確定 UIView 是在 intoView 截止那時,所以我們需要創(chuàng)建一個中間類來保存所有的中間條件,這里我們用 ViewMaker 類。

  1. @interface ViewMaker : NSObject 
  2. @property (nonatomic, strong) Class viewClass; 
  3. @property (nonatomic, assign) CGPoint position; 
  4. @property (nonatomic, assign) CGPoint size
  5. @property (nonatomic, strong) UIColor *color; 
  6. @end 

 

另外我們可以注意到AllocA是一個函數(shù),而UIView無法直接傳遞到這個函數(shù)中,語法就要變成 AllocA([UIView class]) 而失去了簡潔性。所以我們需要先定義一個宏來“吞”掉中括號和 class 這個方法:

  1. #define AllocA(aClass)  alloc_a([aClass class]) 
  2.  
  3. ViewMaker* alloc_a(Class aClass){ 
  4.     ViewMaker *maker = ViewMaker.new; 
  5.     maker.viewClass = aClass; 
  6.     return maker; 

 

(2) 助詞

很多時候,為了讓 DSL 的語法看起來更加連貫,我們需要一些助詞來幫助,例如 Masonry 里面的 make.top.equalTo(superview.mas_top).with.offset(padding.top) 這句中的 with 就是這樣一個助詞。

而這個助詞和我們學(xué)過的語法一樣,通常沒有什么實(shí)際效果,簡單返回self就可以。

  1. @interface ViewMaker : NSObject 
  2. @property (nonatomic, strong) Class viewClass; 
  3. @property (nonatomic, assign) CGPoint position; 
  4. @property (nonatomic, assign) CGPoint size
  5. @property (nonatomic, strong) UIColor *color; 
  6. @property (nonatomic, readonly) ViewMaker *with
  7. @end 
  8.  
  9. @implementation ViewMaker 
  10.  
  11. - (ViewMaker *)with 
  12.     return self; 
  13. @end 

 

需要注意的是,返回自己,就沒有辦法阻止用戶不斷調(diào)用自己 .with.with.with ,為了避免這種情況,可以新生成一個類,每個類都擁有自己所在層次的方法,避免躍層調(diào)用。

  1. @interface ViewMaker : NSObject 
  2. @property (nonatomic, strong) Class viewClass; 
  3. @property (nonatomic, assign) CGPoint position; 
  4. @property (nonatomic, assign) CGPoint size
  5. @property (nonatomic, strong) UIColor *color; 
  6. @end 
  7.  
  8. @interface ViewClassHelper : NSObject 
  9. @property (nonatomic, strong) Class viewClass; 
  10. @property (nonatomic, readonly) ViewMaker *with
  11. @end 
  12.  
  13. #define AllocA(aClass)  alloc_a([aClass class]) 
  14.  
  15. ViewClassHelper* alloc_a(Class aClass){ 
  16.     ViewClassHelper *helper = ViewClassHelper.new; 
  17.     helper.viewClass = aClass; 
  18.     return helper; 
  19. @implementation ViewClassHelper 
  20.  
  21. - (ViewMaker *)with 
  22.     ViewMaker *maker = ViewMaker.new; 
  23.     maker.viewClass = self.viewClass; 
  24.     return maker; 
  25. @end 

 

這樣就有效防止了,.with.with.with這樣的語法。但是實(shí)際上,我們要根據(jù)真實(shí)的需要來進(jìn)行開發(fā),使用 DSL 的用戶是為了更好的表達(dá)性,所以并不會寫出.with.with.with這樣的代碼,這樣的防護(hù)性措施就顯得有點(diǎn)不必要了。

不過使用類來區(qū)分助詞還有另外幾個小好處,就是它可以確保在語法提示的時候,ViewClassHelper這個類只有.with這樣一個語法提示,而ViewMaker不出現(xiàn).with語法提示;并且同時確保.with一定要出現(xiàn)。

不過為了簡化文章,我們都使用前者,既.with返回self來繼續(xù)下文:

  1. @interface ViewMaker : NSObject 
  2. @property (nonatomic, strong) Class viewClass; 
  3. @property (nonatomic, assign) CGPoint position; 
  4. @property (nonatomic, assign) CGPoint size
  5. @property (nonatomic, strong) UIColor *color; 
  6. @property (nonatomic, readonly) ViewMaker *with
  7. @end 
  8.  
  9. @implementation ViewMaker 
  10.  
  11. - (ViewMaker *)with 
  12.     return self; 
  13. @end 

 

(3) 修飾部分——定語

像例子中的position size bgColor這些都是定語部分,用來修飾UIView,他們以屬性的形勢存在于ViewMaker的實(shí)例中,為了支持鏈?zhǔn)奖磉_(dá),所以實(shí)現(xiàn)的時候,都會繼續(xù)返回self。

我們來試著實(shí)現(xiàn)下:

  1. @interface ViewMaker : NSObject 
  2. // ... 
  3. @property (nonatomic, copy) ViewMaker* (^position)(CGFloat x, CGFloat y); 
  4. @property (nonatomic, copy) ViewMaker* (^size)(CGFloat x, CGFloat y); 
  5. @property (nonatomic, copy) ViewMaker* (^bgColor)(UIColor *color); 
  6. @end 
  7.  
  8. @implementation ViewMaker 
  9.  
  10. - (instancetype)init 
  11.     if (self = [super init]) { 
  12.         @weakify(self) 
  13.         _position = ^ViewMaker *(CGFloat x, CGFloat y) { 
  14.             @strongify(self) 
  15.             self.position = CGPointMake(x, y); 
  16.         }; 
  17.         _size = ^ViewMaker *(CGFloat x, CGFloat y) { 
  18.             @strongify(self) 
  19.             self.size = CGPointMake(x, y); 
  20.         }; 
  21.         _bgColor = ^ViewMaker *(UIColor *color) { 
  22.             @strongify(self) 
  23.             self.color = color; 
  24.         }; 
  25.     } 
  26.     return self; 
  27. @end 

 

(4) 終結(jié)詞

“終結(jié)詞”這個實(shí)在是在現(xiàn)代語法里面找不到對應(yīng)關(guān)系了,但是在 DSL 中,這一段尤為重要。ViewMaker的實(shí)例從頭至尾收集了很多的修飾,需要***的一個表達(dá)詞語來產(chǎn)生***的結(jié)果,這里就稱為”終結(jié)詞”。例如在 Expecta 這個開源庫里面的 equal 就是把真正的行為表現(xiàn)出來的時候,to 和 notTo 都不會真正觸發(fā)行為。

在我們的例子里,終結(jié)詞.intoView(aSuperViwe)可以這樣實(shí)現(xiàn):

  1. @interface ViewMaker : NSObject 
  2. // ... 
  3. @property (nonatomic, copy) UIView* (^intoView)(UIView *superView); 
  4. @end 
  5.  
  6. @implementation ViewMaker 
  7.  
  8. - (instancetype)init 
  9.     if (self = [super init]) { 
  10.         @weakify(self) 
  11.         // ... 
  12.         _intoView = ^UIView *(UIView *superView) { 
  13.             @strongify(self) 
  14.             CGRect rect = CGRectMake(self.position.x, self.position.y, 
  15.                          self.size.width, self.size.height); 
  16.             UIView *view = [[UIView alloc] initWithFrame:rect]; 
  17.             view.backgroundColor = self.color; 
  18.             [superView addSubView:view]; 
  19.             return view
  20.         }; 
  21.     } 
  22.     return self; 
  23. @end 

 

這樣,一個終結(jié)詞就寫好了。

最終代碼的匯總:

  1. @interface ViewMaker : NSObject 
  2. @property (nonatomic, strong) Class viewClass; 
  3. @property (nonatomic, assign) CGPoint position; 
  4. @property (nonatomic, assign) CGPoint size
  5. @property (nonatomic, strong) UIColor *color; 
  6. @property (nonatomic, readonly) ViewMaker *with
  7. @property (nonatomic, copy) ViewMaker* (^position)(CGFloat x, CGFloat y); 
  8. @property (nonatomic, copy) ViewMaker* (^size)(CGFloat x, CGFloat y); 
  9. @property (nonatomic, copy) ViewMaker* (^bgColor)(UIColor *color); 
  10. @property (nonatomic, copy) UIView* (^intoView)(UIView *superView); 
  11. @end 
  12.  
  13. @implementation ViewMaker 
  14.  
  15. - (instancetype)init 
  16.     if (self = [super init]) { 
  17.         @weakify(self) 
  18.         _position = ^ViewMaker *(CGFloat x, CGFloat y) { 
  19.             @strongify(self) 
  20.             self.position = CGPointMake(x, y); 
  21.         }; 
  22.         _size = ^ViewMaker *(CGFloat x, CGFloat y) { 
  23.             @strongify(self) 
  24.             self.size = CGPointMake(x, y); 
  25.         }; 
  26.         _bgColor = ^ViewMaker *(UIColor *color) { 
  27.             @strongify(self) 
  28.             self.color = color; 
  29.         }; 
  30.         _intoView = ^UIView *(UIView *superView) { 
  31.             @strongify(self) 
  32.             CGRect rect = CGRectMake(self.position.x, self.position.y, 
  33.                          self.size.width, self.size.height); 
  34.             UIView *view = [[UIView alloc] initWithFrame:rect]; 
  35.             view.backgroundColor = self.color; 
  36.             [superView addSubView:view]; 
  37.             return view
  38.         }; 
  39.     } 
  40.     return self; 
  41.  
  42. - (ViewMaker *)with 
  43.     return self; 
  44. @end 

總結(jié)

這種鏈?zhǔn)秸{(diào)用能夠使程序更加清晰,在特定場景下使程序的可讀性更強(qiáng)。這種手段在Swift也是相同道理,大家可以善加利用,讓自己的代碼更加美觀。

其實(shí),iOS 開發(fā)者要想不斷精進(jìn),成長為真正的大牛高手,必須將自己的視野凌駕于業(yè)務(wù)需求之上,精簡強(qiáng)化核心技能,提升自己對語言和工具的掌握層次,才能提高開發(fā)效率,提升技能水平。

這里為你準(zhǔn)備了更多好玩的,讓你事半功倍的 iOS 高階黑魔法攻防術(shù),斯達(dá)克學(xué)院(StuQ ) 特別邀請備受學(xué)員喜愛的資深 iOS 技術(shù)專家臧成威老師開設(shè)《 iOS 實(shí)戰(zhàn)黑魔法 》課程,6周12小時高效 Get iOS 必須掌握的高階黑魔法攻防術(shù),讓你從普通的開發(fā)者中漸漸走出來,看到一個不一樣的語言,感受不一樣的開發(fā)!

責(zé)任編輯:龐桂玉 來源: iOS開發(fā)by唐巧
相關(guān)推薦

2011-07-26 09:19:27

Objective-C 重載

2011-08-10 18:07:29

Objective-C反射

2013-06-20 10:40:32

Objective-C實(shí)現(xiàn)截圖

2013-03-27 12:54:00

iOS開發(fā)Objective-C

2011-05-11 15:58:34

Objective-C

2011-05-11 11:20:26

Objective-C

2011-08-16 17:43:47

Objective-C內(nèi)存管理Autorelease

2011-07-27 16:18:42

Objective-c 協(xié)議

2011-08-04 15:55:50

Windows 編譯 Objective-

2011-05-11 13:54:08

Objective-C

2011-05-11 15:45:50

內(nèi)存管理Objective-C

2011-08-02 13:16:36

Objective-C 語法 函數(shù)

2011-08-04 11:15:46

Objective-C 構(gòu)造函數(shù) 構(gòu)造方法

2011-05-11 14:06:49

Objective-C

2011-08-04 14:58:37

Objective-C Cocoa NSString

2013-08-21 14:57:42

objective-c問題

2011-07-25 14:27:10

Objective-C 協(xié)議 函數(shù)

2011-08-03 16:55:05

Objective-C 代理

2011-08-04 09:35:09

Objective-C 編碼規(guī)范

2014-04-30 10:16:04

Objective-CiOS語法
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號

主站蜘蛛池模板: 日韩不卡在线观看 | 国产精品久久久久久久久久久久久 | 日韩电影中文字幕在线观看 | 激情欧美日韩一区二区 | 国产精品一区二区电影 | 国产一区二区精 | 国产亚洲成av人片在线观看桃 | 中文字幕免费在线 | 在线日韩视频 | 欧美在线一区二区三区四区 | 2018国产大陆天天弄 | 美女一级a毛片免费观看97 | 久久看精品 | 日本欧美大片 | 国产精品视频久久久久 | 在线欧美视频 | 91久久久久久| 日本网站在线看 | 四虎影院一区二区 | 小川阿佐美pgd-606在线 | 99精品视频在线观看 | 色888www视频在线观看 | 亚洲劲爆av| 日韩在线不卡视频 | 精品国产欧美一区二区三区成人 | 福利视频1000 | 久久网亚洲 | 欧美福利影院 | 亚洲精品一区av在线播放 | 依人成人 | 国产精品毛片久久久久久久 | 羞羞的视频在线看 | 国产一区二区三区四区区 | 欧美日韩久久 | 一区二区三区免费在线观看 | 精品一二三区在线观看 | 成人字幕网zmw| 日韩不卡在线 | 欧美亚洲成人网 | 羞羞视频在线网站观看 | 国产午夜精品久久久 |