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

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

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

iOS

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

感謝臧成威的授權,以下是文章正文。

背景

在程序開發中,我們總是希望能夠更加簡潔、更加語義化地去表達自己的邏輯,鏈式調用是一種常見的處理方式。我們常用的 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); 

 

像這種用于特定領域的表達方式,我們叫做 DSL (Domain Specific Language),本文就介紹一下如何實現一個鏈式調用的 DSL.

鏈式調用的實現

我們舉一個具體的例子,比如我們用鏈式表達式來創建一個 UIView,設置其 frame、backgroundColor, 并添加至某個父 View。

對于最基本的 Objective-C (在 iOS4 block 出現之前),如果要實現鏈式調用,只能是這個樣子的:

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

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

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

 

可以看出,鏈式語法的語義性很明確,后者的語法更加緊湊,下面我們從兩個角度看一下后者的實現。

1. 從語法層面來看

鏈式調用可以用兩種方式來實現:

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

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

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

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

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

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

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

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

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

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

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

在調用 .position(x, y) 方法時,執行這個block,返回 ViewMaker 的實例保證鏈式調用得以進行。

2. 從語義層面來看

從語義層面上,需要界定哪些是助詞,哪些是需要接受參數的。為了保證鏈式調用能夠完成,需要考慮傳入什么,返回什么。

還是以上面的例子來講:

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

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

下面我們分別從四段來看,如何實現這樣一個表達式:

(1) 賓語

在 AllocA(UIView) 的語義中,我們確定了賓語是 a UIVIew。由于確定 UIView 是在 intoView 截止那時,所以我們需要創建一個中間類來保存所有的中間條件,這里我們用 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是一個函數,而UIView無法直接傳遞到這個函數中,語法就要變成 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 就是這樣一個助詞。

而這個助詞和我們學過的語法一樣,通常沒有什么實際效果,簡單返回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 

 

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

  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這樣的語法。但是實際上,我們要根據真實的需要來進行開發,使用 DSL 的用戶是為了更好的表達性,所以并不會寫出.with.with.with這樣的代碼,這樣的防護性措施就顯得有點不必要了。

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

不過為了簡化文章,我們都使用前者,既.with返回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 

 

(3) 修飾部分——定語

像例子中的position size bgColor這些都是定語部分,用來修飾UIView,他們以屬性的形勢存在于ViewMaker的實例中,為了支持鏈式表達,所以實現的時候,都會繼續返回self。

我們來試著實現下:

  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) 終結詞

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

在我們的例子里,終結詞.intoView(aSuperViwe)可以這樣實現:

  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 

 

這樣,一個終結詞就寫好了。

最終代碼的匯總:

  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 

總結

這種鏈式調用能夠使程序更加清晰,在特定場景下使程序的可讀性更強。這種手段在Swift也是相同道理,大家可以善加利用,讓自己的代碼更加美觀。

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

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

責任編輯:龐桂玉 來源: iOS開發by唐巧
相關推薦

2011-07-26 09:19:27

Objective-C 重載

2011-08-10 18:07:29

Objective-C反射

2013-06-20 10:40:32

Objective-C實現截圖

2013-03-27 12:54:00

iOS開發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內存管理Autorelease

2011-07-27 16:18:42

Objective-c 協議

2011-08-04 15:55:50

Windows 編譯 Objective-

2011-05-11 13:54:08

Objective-C

2011-05-11 15:45:50

內存管理Objective-C

2011-08-02 13:16:36

Objective-C 語法 函數

2011-08-04 11:15:46

Objective-C 構造函數 構造方法

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 協議 函數

2011-08-03 16:55:05

Objective-C 代理

2011-08-04 09:35:09

Objective-C 編碼規范

2014-04-30 10:16:04

Objective-CiOS語法
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 久久久久久国产精品久久 | 天天射天天干 | 91精品国产一区二区三区 | 亚洲精视频 | 久久一区精品 | 国产一区免费视频 | 成人1区 | 久久鲁视频 | 欧美黑人狂野猛交老妇 | 国内精品久久久久 | 国产精品美女久久久 | 精品一区二区久久久久久久网站 | 最新91在线| 亚洲精品在线播放 | 国产精品久久久久无码av | 精品免费国产一区二区三区 | 艹逼网 | 国产午夜精品一区二区三区在线观看 | 成年人黄色小视频 | 日韩精品在线播放 | 国产精品区一区二区三 | 狠狠夜夜 | 日韩在线小视频 | 黄免费观看视频 | 亚洲高清在线观看 | 美女啪啪国产 | 污视频在线免费观看 | 日本一二三区在线观看 | 国产精品久久久久久久久久免费 | 亚洲精品自在在线观看 | 国产精品视频专区 | 国产精品日韩欧美一区二区三区 | 精品福利一区二区三区 | 一级做a爰片性色毛片视频停止 | 欧美成年视频 | 午夜精品久久久 | 欧美在线播放一区 | 欧美日韩精品久久久免费观看 | 国产成人综合一区二区三区 | 国产精品无码久久久久 | 草久久免费视频 |