主 Bundle 欄
調(diào)試區(qū)截屏
Masonry 中的鏈?zhǔn)骄幊膛c函數(shù)式編程代碼范例:
//雖然我知道可能沒有這么簡單,但是宏觀理解來看的話:
//點(diǎn)語法 : --> 鏈?zhǔn)骄幊?//小括號(hào) : --> 函數(shù)式編程
[self.view mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo (self.view) ;
make.left.equalTo (self.view) ;
make.width.mas_equalTo (@40) ;
make.height.mas_equalTo (@40) ;
}] ;
ViewController.h 文件
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
ViewController.m 文件
#import "ViewController.h"
#import "Person.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//創(chuàng)建 person 對(duì)象:
Person *person = [[Person alloc] init] ;
//這就是函數(shù)式編程與鏈?zhǔn)骄幊痰慕Y(jié)合實(shí)例:點(diǎn)語法的遍歷和小括號(hào)的使用!就醬!
person.run2().eat2().run2().eat2() ;
//含參的鏈?zhǔn)骄幊檀a:
person.run3(5).eat3(@"管氏翅吧") ;
}
@end
Person.h 文件
#import <Foundation/Foundation.h>
@interface Person : NSObject
- (void)run ;
- (void)eat ;
- (Person *)run1 ;
- (Person *)eat1 ;
//哪里能用個(gè)小括號(hào)?!小括號(hào)就是函數(shù)式編程中的函數(shù)!
//真正的函數(shù)式編程的體現(xiàn)是因?yàn)榉祷氐氖且粋€(gè) block!
- (Person *(^)())run2 ;
- (Person *(^)())eat2 ;
//含參 block:
- (Person *(^)(NSString *food))eat3 ;
- (Person *(^)(int distance))run3 ;
@end
Person.m 文件
#import "Person.h"
@implementation Person
//無參 block 的 run2:
- (Person *(^)())run2 {
//創(chuàng)建一個(gè) Person 類型的 block 對(duì)象并返回對(duì)象:
Person *(^run2Block)() = ^() {
NSLog(@"我就是 run2Block") ;
//返回對(duì)象:
return self ;
} ;
//要求返回的是一個(gè) block 值,而且這個(gè) block 是 Person 類型的對(duì)象:
return run2Block ;
}
//無參 block 的 eat2:
- (Person *(^)())eat2 {
return ^() {
NSLog(@"我就是 eat2Block") ;
return self ;
} ;
}
//含參 block 的 eat3:
- (Person *(^)(NSString *food))eat3 {
return ^(NSString *food) {
NSLog(@"吃的是 %@" , food) ;
return self ;
} ;
}
//含參 block 的 run3:
- (Person *(^)(int distance))run3 {
return ^(int distance) {
NSLog(@"今晚我跑了 %d 千米" , distance) ;
return self ;
} ;
}
@end