iOS_Block的用法

https://www.jianshu.com/p/18d446589589
block的本质其实就是OC对象,

Block是oc的一段代码块,在需要的时候调用。

Block变量的声明格式为: 返回值类型(^Block名字)(参数列表);

// 声明一个无返回值,参数为两个字符串对象的block
void(^block)(NSString *str1, NSString *str2);

// 形参变量名称可以省略,只留有变量类型
void(^block)(NSString *, NSString *);

// ARC下直接使用strong,MRC下使用copy
@property(nonatomic, strong)void(^eat)();

// inlineBlock出来的block

<#returnType#>(^<#blockName#>)(<#parameterTypes#>) = ^(<#parameters#>) {
        <#statements#>
    };

// 完整的格式

<#returnType#>(^<#blockName#>)(<#parameterTypes#>) = ^<#returnType#>(<#parameters#>) {
            <#statements#>
        };

例子:

// 报错代码
id(^block)(int a, int b) = ^(int a, int b) {
        return nil;
    };
// 正确代码
id(^block)(int a, int b) = ^id(int a, int b) {
        return nil;
    };
block的三种用法
  1. block作为属性使用
@property(nonatomic, strong)void(^eat)();
实现
Person *p = [[Person alloc] init];
    p.eat = ^(){
        NSLog(@"laiel");
    };
    _p = p;

调用
    _p.eat();
  1. block作为参数
// 声明
+ (NSURLSessionDataTask *)PostDataRequestWithParameters:(id)parameters PostRequestActionType:(PostRequestActionType)requestActionType Block:(void (^)(NSDictionary *result, NSError *error))block;


+ (NSURLSessionDataTask *)PostDataRequestWithParameters:(id)parameters PostRequestActionType:(PostRequestActionType)requestActionType Block:(void (^)(NSDictionary *result, NSError *error))block
{
    NSDictionary *dict = [PostUrlParameter requestUrlWithAction:requestActionType parameters:parameters];
    
    return [[APIClient sharedClient] POST:[dict objectForKey:@"requestUrl"] parameters:[dict objectForKey:@"parameters"] progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (block) {
            block(responseObject, nil);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (block) {
            block([self errorDict], error);
        }
    }];
}

3.block作为函数返回值(实现链式编程,函数调用的前提是具有返回对象)

@interface Person : NSObject

//ARC使用strong,非ARC使用copy
@property(nonatomic, strong)void(^eat)();

- (void(^)(int ))run;

@end
#import "Person.h"

@implementation Person

- (void (^)(int))run
{
    return ^(int a ){
        NSLog(@"执行%d", a);
    };
}
@end
调用
Person *p = [[Person alloc] init];
p.run(10);
masony的原理:

运用链式编程思想,方法的返回值就是方法的调用者!

代码示例:
新建一个Add类别

#import 
#import "JeyAddManager.h"

@interface NSObject (Add)

+ (int)Jey_make:(void(^)(JeyAddManager *))block;

@end
#import "NSObject+Add.h"

@implementation NSObject (Add)

+ (int)Jey_make:(void (^)(JeyAddManager *))block
{
    JeyAddManager *mgr = [[JeyAddManager alloc] init];
    
    block(mgr);
    return mgr.result;
}
@end

一个管理类

#import 

@interface JeyAddManager : NSObject
@property (nonatomic, assign) int result;

//- (instancetype)add:(int)value;

// 使用block写法
//- (void(^)(int))add;

- (JeyAddManager *(^)(int))add;

@end
#import "JeyAddManager.h"

@implementation JeyAddManager

//- (instancetype)add:(int)value
//{
//    _result += value;
//    return self;
//}

//- (void (^)(int))add{
//
//    return ^(int m){
//        _result += m;
//    };
//}

- (JeyAddManager *(^)(int))add
{
    return ^(int m){
        _result += m;
        return self;
    };
}

@end

方法调用

int result = [NSObject Jey_make:^(JeyAddManager *mgr) {
       mgr.add(10).add(9).add(10);
    }];
    NSLog(@"%d",result);

你可能感兴趣的:(iOS_Block的用法)