2-1RAC简介-BLock块三种常用方式

BLock块三种常用方式

11.block作为对象的属性!!

#import "ViewController.h"
#import "Person.h"

@interface ViewController ()
/**  */
@property(strong ,nonatomic)Person * p;
@end

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    //1.block作为对象的属性!!
    [self block1];
    }

-(void)block1{
    Person * p = [[Person alloc]init];
    
    //block  -- inlineBlock
    void(^HKBlock)() = ^() {
        NSLog(@"block");
    };
    
    p.block = HKBlock;
    _p = p;
    

}
@end
#import 

@interface Person : NSObject

/** block :ARC使用strong  非ARC copy  */
@property(nonatomic,strong) void(^block)();
@end

2.block作为方法的参数!!

- (void)viewDidLoad {
    [super viewDidLoad];
    //1.block作为对象的属性!!
    [self block2];
}
-(void)block2{
    Person * p = [[Person alloc]init];
    [p eat:^(NSString * s) {
        NSLog(@"爱吃%@",s);
    }];

}
#import 
@interface Person : NSObject
-(void)eat:(void(^)(NSString *))block;
@end
-(void (^)(int))run
{
    return ^(int m){
        NSLog(@"哥么跑起来了!!跑了%d",m);
    };
}

//3.block作为返回值!!!(扩展非常强!!)

#import "ViewController.h"
#import "Person.h"
@interface ViewController ()
@end
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
     //3.block作为返回值!!!(扩展非常强!!)
    Person * p = [[Person alloc]init];
    p.run(100);
}
#import 
@interface Person : NSObject
-(void(^)(int))run;
#import "Person.h"

@implementation Person
-(void (^)(int))run
{
    return ^(int m){
        NSLog(@"哥么跑起来了!!跑了%d",m);
    };
}
@end

你可能感兴趣的:(2-1RAC简介-BLock块三种常用方式)