美文网首页
ios链式开发 oc 移除项目中的delegate,让bloc

ios链式开发 oc 移除项目中的delegate,让bloc

作者: joymake | 来源:发表于2016-10-17 16:31 被阅读96次

    之前发现了一种类似block串行执行的开发方式,今天终于不忙了就简单写一下做个笔记


    链式调用

    其实很简单,需要注意的就是相应对象的返回值,如下拿 person 和boy两个对象举例,最后实现 person.person(NO).strongMan(@"强壮的小男孩").boy(@"小男孩儿开始说").speak(@"我想回家");

    Person.h

    #import <Foundation/Foundation.h>
    @class Boy;
    @interface Person : NSObject
    @property (nonatomic,readonly) Person *(^person)(BOOL enable);
    @property (nonatomic,readonly) Person *(^strongMan)(NSString *str);
    @property (nonatomic,readonly) Boy *(^boy)(NSString *str);
    @end
    

    Person.m

    #import "Person.h"
    #import "Boy.h"
    @implementation Person
    -(Person *(^)(BOOL))person{
        return ^(BOOL enable){
            NSLog(@"Person Say:%d\n",enable);
            return self;
        };
    }
    
    -(Person *(^)(NSString *))strongMan{
        return ^(NSString *str){
            NSLog(@"%@\n",str);
            return self;
        };
    }
    
    -(Boy *(^)(NSString *))boy{
        return ^(NSString *str){
            NSLog(@"%@\n",str);
            return [[Boy alloc]init];
        };
    }
    @end
    

    boy.h

    #import <Foundation/Foundation.h>
    @interface Boy : NSObject
    @property (nonatomic,readonly)Boy *(^speak)(NSString *speakStr);
    @end
    

    boy.m

    #import "Boy.h"
    @implementation Boy
    -(Boy *(^)(NSString *))speak{
        return ^(NSString *speakStr){
            NSLog(@"speak:%@\n",speakStr);
            return self;
        };
    }
    @end
    

    vc调用--------------------------------------------------

    - (void)viewDidLoad {
        [super viewDidLoad];
        person.person(NO).strongMan(@"强壮的小男孩").boy(@"小男孩儿开始说").speak(@"我想回家");
    }
    
    打印结果

    一般的使用到此就结束了,下面来稍微加点难度,前面的我们都看到了,都是正向传值,都是调用方直接告诉被调用方要干什么什么,那么我们如果需要调用方回传一些数据做处理呢,大家自己思考一下再往下看

    思路是这样的,既然要反向且又想链式,那么自然想到了block,我们在被调用方的参数中修改参数为block是否能实现呢,继续往下看

    拿系统定位功能CLLocationManager来做例子,调用定位,我们首先要检查权限、设置参数、开始定位、然后定位代理成功回调处理、失败处理这几个功能,我们最终的实现结果需要是这样的

        self.locationManager.checkAuthorization(NO).startLocation().locationSuccess(^(NSDictionary *addressDict) {
            NSLog(@"定位成功");
        }).locationError(^(NSError *error){
            NSLog(@"定位失败");
        });
    
    

    1.首先我们声明这几个block

    ###JoyLocationManager.h
    @property (nonatomic,readonly)JoyLocationManager *(^checkAuthorization)(BOOL backGroundModel);
    @property (nonatomic,readonly)JoyLocationManager *(^startLocation)();
    @property (nonatomic,readonly)JoyLocationManager *(^stopLocation)();
    //参数为idblock类型(我这是定义的id类型参数的回调)
    @property (nonatomic,copy)JoyLocationManager    *(^locationSuccess)(IDBLOCK block);
    //参数为idblock类型(我这是定义的error类型参数的回调)
    @property (nonatomic,copy)JoyLocationManager    *(^locationError)(ERRORBLOCK block);
    
    2.这几个block
    ###JoyLocationManager.m
    #pragma mark 检查权限
    -(JoyLocationManager *(^)(BOOL))checkAuthorization{
        return ^(BOOL backGroundModel){
            [self checkAuthorStateAndSetBackModel:backGroundModel];//检查权限并设置是否后台运行模式
            return self;
        };
    }
    
    
    #pragma mark 开始定位
    -(JoyLocationManager *(^)())startLocation{
        return ^(){
            [self startManagerLocation];//开始定位
            return self;
        };
    }
    
    
    #pragma mark 结束定位
    -(JoyLocationManager *(^)())stopLocation{
        return ^(){
            [self stopManagerLocation];  //结束定位
            return self;
        };
    }
    
    #pragma mark 定位成功
    -(JoyLocationManager *(^)(IDBLOCK block))locationSuccess{
        return ^(IDBLOCK block){
    //runtime关联对象,定位成功代理中回调参数
            objc_setAssociatedObject(self, _cmd, block, OBJC_ASSOCIATION_COPY);
            return self;
        };
    }
    
    #pragma mark 定位失败
    -(JoyLocationManager *(^)(ERRORBLOCK))locationError{
        return ^(ERRORBLOCK block){
    //runtime关联对象,定位失败代理中回调参数
            objc_setAssociatedObject(self, _cmd, block, OBJC_ASSOCIATION_COPY);
            return self;
        };
    }
    
    
    #pragma mark 定位代理   
    //CLLocation这个类里面包括的一些常用的位置信息有经度、纬度、海拔、速度、精确度
    -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
      //定位成功进行地理反编码,可以改进一下考虑自行加入到链中
        [self reverseGEOCodeLocation:locations.firstObject];
        [self stopManagerLocation];
    }
    
    //定位失败
    -(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
    //拿到前面关联的block并回调参数
        ERRORBLOCK successBlock = objc_getAssociatedObject(self, @selector(locationError));
        successBlock?successBlock(error):nil;
    }
    
    //反地理编码
    - (void)reverseGEOCodeLocation:(CLLocation *)location{
        CLGeocoder *revGeo = [[CLGeocoder alloc] init];
        [revGeo reverseGeocodeLocation:location
                     completionHandler:^(NSArray *placemarks, NSError *error) {
                         if (!error && [placemarks count] > 0)
                         {
                             NSDictionary *dict =
                             [[placemarks objectAtIndex:0] addressDictionary];
                             IDBLOCK successBlock = objc_getAssociatedObject(self, @selector(locationSuccess));
                             successBlock?successBlock(dict):nil;
                         }else{
                             NSLog(@"ERROR: %@", error);
                         }
                     }];
    }
    

    到此可以试试了

    self.locationManager.checkAuthorization(NO).startLocation().locationSuccess(^(NSDictionary *addressDict) {
            NSLog(@"定位成功");
        }).locationError(^(NSError *error){
            NSLog(@"定位失败");
        });
    

    成功回走成功的回调,失败会走失败的回调,大家可以自行去试试把定位精度、headupdate、地理编码等试试加进来

    相关文章

      网友评论

          本文标题:ios链式开发 oc 移除项目中的delegate,让bloc

          本文链接:https://www.haomeiwen.com/subject/zgfzyttx.html