[推薦表驅動法]揪阶!
優(yōu)點:
1> 用數(shù)據(jù)代替邏輯遣总,容易維護
2> 可以把表中的數(shù)據(jù)存放在文件中睬罗,運行時讀取轨功,減少代碼體量。數(shù)據(jù)變更時只需要修改文件
3> 降低復雜度
場景1 根據(jù)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";
}
可以看出邏輯是有規(guī)律傅物,那么怎么去掉if-else呢夯辖,可以換個方式實現(xiàn)
用表驅動方法直接訪問得到一樣的結果
NSDictionary *dic = @{
@"0":@"0000",
@"1":@"1111",
@"2":@"2222",
@"3":@"3333",
@"4":@"4444",
@"5":@"5555",
@"6":@"6666",
};
result = dic[status]
場景2 進行tableview點擊方法中的跳轉
一般寫法 根據(jù)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];
}
用表驅動法優(yōu)化后的代碼
//只需要維護這個字典即可
NSArray *arr = @[
@{
//跳轉的類名
@"only_className":@"ViewController",
//攜帶的參數(shù)
@"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];
}
索引訪問表優(yōu)化代碼
// 同樣管理字典即可
NSDictionary *dataDic = @{
//唯一判斷標志
@"1":@{
//方法名
@"mySelectorName":@"fitnessAction:",
//傳入的參數(shù)
@"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 根據(jù)數(shù)值判斷屬于哪個范圍
0-59是差 60-69是及格 70-79是中等 80-89是良好 90 - 99分是優(yōu)秀 100是特優(yōu)
//一般思路
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(@"優(yōu)秀");
}else{
NSLog(@"特優(yōu)");
}
這種帶范圍的判斷可以用表驅動法中的階梯訪問
NSArray *gradeArr = @[@(59),@(69),@(79),@(89),@(99),@(100)];
NSArray *levelArr = @[@"差",@"及格",@"中等",@"良好",@"優(yōu)秀",@"特優(yōu)"];
for( int i = 0 ; i < gradeArr.count ; i++){
if(grade <= [gradeArr[i] integerValue]){
NSLog(@"%@",levelArr[i]);
return levelArr[i];
}
}