链式语法的使用

来源:互联网 发布:宾馆管理系统数据库 编辑:程序博客网 时间:2024/06/02 10:29

目的:为了使得代码更加容易读懂

 _client = [[IZKJHttpsClientalloc]init];


    SoapSuccess success = ^(id_Nullable data) {

        __strongtypeof(weakwelf) stronglyself =weakwelf;

        // DaLog(@"%@",data);

        if (!stronglyself)return ;

        NSArray* dicar = (NSArray*) data;


        for (NSDictionary*dicin dicar) {

            InformationModel * info = [[InformationModelalloc]initWithResponseDic:dic];

            [stronglyself.tabelModelaraddObject:info];

        }


        [stronglyself.tableViewreloadData];

    };

    SoapFail failure = ^(id_Nullable faildata) {


    };

    _client.codeandNecInfoForFetch(@"0103",info)

           .success(success)

           .failure(failure)

           .resume();


  等同于如下写法_client.codeandNecInfoForFetch(@"0103",info).success(success).failure(failure).resume();


来看实现文件.h

- (IZKJHttpsClient*(^)(NSString *code,NSString *info))codeandNecInfoForFetch;

- (IZKJHttpsClient*(^)(NSString *code,NSString *info))codeandNecInfoForPush;

- (IZKJHttpsClient*(^)(SoapSuccess))success;

- (IZKJHttpsClient*(^)(SoapFail))failure;

- (IZKJHttpsClient*(^)())resume;

以及.m

- (IZKJHttpsClient *(^)(NSString *,NSString *))codeandNecInfoForFetch {

    return ^IZKJHttpsClient*(NSString *code,NSString *info) {

        self.soapMsg = [selfpreparefetchSoapMsg:codeNecInfo:info];

        self.accessway =KaccessWaypost;

        returnself;

    };

}

- (IZKJHttpsClient *(^)(NSString *,NSString *))codeandNecInfoForPush {

    return ^IZKJHttpsClient*(NSString *code,NSString *info) {

        self.soapMsg = [selfpreparepushSoapMsg:code NecInfo:info];

        self.accessway =KaccessWayupdate;

        returnself;

    };

}

- (IZKJHttpsClient *(^)(SoapSuccess))success {

    return ^IZKJHttpsClient*(SoapSuccess sucessblock) {

        self.successBlock = sucessblock;

        returnself;

    };

}

- (IZKJHttpsClient *(^)(SoapFail))failure {

    return ^IZKJHttpsClient*(SoapFail failblock) {

        self.failBlock = failblock;

        returnself;

    };

}

-(IZKJHttpsClient *(^)())resume {

    return ^IZKJHttpsClient*() {

//得到所有网络访问必须的要素开始工作

        [selfstartWork];


        returnself;

    };

}

显然  因为在此语法中  方法返回的对象是自身,而这个自身是带有block回调块的

并且在方法中实现了block回调块的内容,其实是携带了自身赋值和传值功能的


因此

_client.codeandNecInfoForFetch(@"0103",info).success(success).failure(failure).resume();


这种写法就可以不同的通过回调自身来达到链式传入数值在自身的类里面对该数值进行处理的效果


而给出的例子实际上在最后一次的resume中的方法是执行了相应的网络访问的请求

而回调的block代码块则是前面的SoapSuccessSoapFail  

就会回到之前的方法来对下载来的数据进行处理的逻辑


0 0
原创粉丝点击