iOS

如何优化庞大的 "if else" 逻辑语句

2021-09-03  本文已影响0人  _VisitorsZsl
[推荐表驱动法]!
优点:
1> 用数据代替逻辑,容易维护
2> 可以把表中的数据存放在文件中,运行时读取,减少代码体量。数据变更时只需要修改文件
3> 降低复杂度

场景1 根据status的值判断对应的result的值:

   if ([status isEqualToString:@"0"]) {
       result = @"0000";
   }else if ([status isEqualToString:@"1"]){
       result = @"1111";
   }else if ([status isEqualToString:@"2"]){
       result = @"2222";
  }else if ([status isEqualToString:@"3"]){
       result = @"3333";
  }else if ([status isEqualToString:@"4"]){
       result = @"4444";
  }else if ([status isEqualToString:@"5"]){
       result = @"5555";
  }else if ([status isEqualToString:@"6"]){
       result = @"6666";
  }

可以看出逻辑是有规律,那么怎么去掉if-else呢,可以换个方式实现
用表驱动方法直接访问得到一样的结果

NSDictionary *dic = @{
                      @"0":@"0000",
                      @"1":@"1111",
                      @"2":@"2222",
                      @"3":@"3333",
                      @"4":@"4444",
                      @"5":@"5555",
                      @"6":@"6666",
                      };
 result =  dic[status]

场景2 进行tableview点击方法中的跳转

一般写法 根据indexPath.row判断或者判断对应model的属性

if (indexPath.row == 0) {
     ViewController *vc1 = [ViewController new];
     vc1.index = 2;
     [weakSelf.navigationController pushViewController:vc1 animated:YES];
}else if (indexPath.row == 1){
     ViewController *vc2 = [ViewController new];
     vc2.index = 2;
     [weakSelf.navigationController pushViewController:vc2 animated:YES];
}else if (indexPath.row == 3){
     ViewController *vc3 = [ViewController new];
     vc3.index = 2;
     [weakSelf.navigationController pushViewController:vc3 animated:YES];
}else if (indexPath.row == 3){
     ViewController *vc4 = [ViewController new];
     vc4.index = 2;
     [weakSelf.navigationController pushViewController:vc4 animated:YES];
}else if (indexPath.row == 4){
     ViewController *vc5 = [ViewController new];
     vc5.index = 2;
     [weakSelf.navigationController pushViewController:vc5 animated:YES];
 }

用表驱动法优化后的代码

 //只需要维护这个字典即可
 NSArray *arr = @[
      @{
        //跳转的类名
         @"only_className":@"ViewController",
         //携带的参数
         @"index":@(2),
      },
      @{
          @"only_className":@"ViewController",
          @"index":@(2),
      },
      @{
           @"only_className":@"ViewController",
           @"index":@(2),
      },
      @{
           @"only_className":@"ViewController",
           @"index":@(2),
      },
      @{
          @"only_className":@"ViewController",
          @"index":@(2),
      },
 ];
NSDictionary *keyDic = arr[indexPath.row];
YHBaseViewController *vc = [[NSClassFromString((NSString *) keyDic[@"only_className"])alloc] init];
[self.navigationController pushViewController:vc animated:YES];

场景3 每天做不同的事情 (1号健身 2号钓鱼 3号看NBA 4号敲代码 5号休息 6号睡觉 )

一般写法,if(1号) doSomething if(2号) doSomething

 wBaseModel *myModel = model;
 if ([myModel.imageName isEqualToString:@"1"]) {
         [self fitnessAction:@{
               @"paramOne":@"去健身",
         }] ;
 }else if ([myModel.imageName isEqualToString:@"2"]) {
          [self fishingAction];
 }else if ([myModel.imageName isEqualToString:@"3"]) {
          [self WatchNBAAction];
 }else if ([myModel.imageName isEqualToString:@"4"]) {
          [self codeAction];
 }else if ([myModel.imageName isEqualToString:@"5"]) {
          [self releaxAction];
 }else if ([myModel.imageName isEqualToString:@"6"]) {
          [self sleepAction];
 }

索引访问表优化代码

 // 同样管理字典即可
NSDictionary *dataDic = @{
                          //唯一判断标志
                          @"1":@{
                                  //方法名
                                  @"mySelectorName":@"fitnessAction:",
                                  //传入的参数
                                  @"paramOne":@"去健身",
                                  },
                          @"2":@{
                                  @"mySelectorName":@"fishingAction",
                                  },
                          @"3":@{
                                  @"mySelectorName":@"WatchNBAAction",
                                  },
                          @"4":@{
                                  @"mySelectorName":@"codeAction",
                                  },
                          @"5":@{
                                  @"mySelectorName":@"releaxAction:",
                                  },
                          @"6":@{
                                  @"mySelectorName":@"sleepAction",
                                  },
                          };

NSDictionary *keyDic = dataDic[myModel.imageName];
if (keyDic) {
    id obj = [WMZStrategy performIDSelector:keyDic[@"mySelectorName"] Tagert:self withParam:keyDic];
     if(obj)
    NSLog(@"结果 %@",obj);
}

场景4 根据数值判断属于哪个范围

0-59是差 60-69是及格 70-79是中等 80-89是良好 90 - 99分是优秀 100是特优

//一般思路
NSInteger grade = 30;
if (grade > 0 && grade <= 59) {
     NSLog(@"差");
}else if (grade>59&&grade<70) {
     NSLog(@"及格");
}else if (grade>=70&&grade<80) {
     NSLog(@"中等");
}else if (grade>=80&&grade<90) {
     NSLog(@"良好");
}else if (grade>=90&&grade<100) {
      NSLog(@"优秀");
}else{
      NSLog(@"特优");
}

这种带范围的判断可以用表驱动法中的阶梯访问

NSArray *gradeArr = @[@(59),@(69),@(79),@(89),@(99),@(100)];
NSArray *levelArr = @[@"差",@"及格",@"中等",@"良好",@"优秀",@"特优"];
for( int i = 0 ; i < gradeArr.count ; i++){
 if(grade <= [gradeArr[i] integerValue]){
       NSLog(@"%@",levelArr[i]);
        return  levelArr[i];
  }
}
举一反三吧 !!! 可能遇到很多类似的 按照这个思路 大家集思广益
上一篇下一篇

猜你喜欢

热点阅读