美文网首页
iOS开发-项目中减少if-else 的使用

iOS开发-项目中减少if-else 的使用

作者: 善良的皮蛋 | 来源:发表于2020-09-17 09:19 被阅读0次

大概是受够了if-else,特意去网上找了资料,参考了很多资料,现在自己做一个总结。

 /** 表驱动 */
    NSArray *arr = @[
        @{
          //跳转类名
            @"classname":@"OneViewController",
            @"testIndex":[NSString stringWithFormat:@"%ld",indexPath.row],
            @"name":@"这是我的名字,哈哈哈",
        },
    @{
             //跳转类名
               @"classname":@"TwoViewController",
               @"index":@"bian",
           },
    ];
    NSDictionary *keyDic = arr[indexPath.row];
    if (keyDic) {
        [FBRuntime runtimePush:keyDic[@"classname"] dic:keyDic nav:self.navigationController];
     }

看了上面的代码很简单吧,现在主要是来说说[FBRuntime runtimePush:keyDic[@"classname"] dic:keyDic nav:self.navigationController];
如果不需要带参数的跳转,其实看前面的代码就可以,如果需要带参数,那就要考虑runtime,自己写了一个类,现在源码贴上.

#import <Foundation/Foundation.h>
#import "AppDelegate.h"

NS_ASSUME_NONNULL_BEGIN

@interface FBRuntime : NSObject

+ (void)runtimePush:(NSString *)vcName dic:(NSDictionary *)dic nav:(UINavigationController *)nav;


+ (BOOL)checkIsExistPropertyWithInstance:(id)instance verifyPropertyName:(NSString *)verifyPropertyName;

@end

NS_ASSUME_NONNULL_END
//
//  FBRuntime.m
#import "FBRuntime.h"
#import <objc/runtime.h>
#import "AppDelegate.h"

@implementation FBRuntime

//runtime跳转

+ (void)runtimePush:(NSString *)vcName dic:(NSDictionary *)dic nav:(UINavigationController *)nav {
    //类名(对象名)
    
    NSString *class = vcName;
    
    const char *className = [class cStringUsingEncoding:NSASCIIStringEncoding];
    Class newClass = objc_getClass(className);
    if (!newClass) {
        //创建一个类
        Class superClass = [NSObject class];
        newClass = objc_allocateClassPair(superClass, className, 0);
        //注册你创建的这个类
        objc_registerClassPair(newClass);
    }
    // 创建对象(写到这里已经可以进行随机页面跳转了)
    id instance = [[newClass alloc] init];
    
    //下面是传值--------------
    
    [dic enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
        if ([self checkIsExistPropertyWithInstance:instance verifyPropertyName:key]) {
            //kvc给属性赋值
            
            [instance setValue:obj forKey:key];
        }else {
            NSLog(@"不包含key=%@的属性",key);
        }
    }];
    [nav pushViewController:instance animated:YES];
    
}
/**
 *  检测对象是否存在该属性
 */
+ (BOOL)checkIsExistPropertyWithInstance:(id)instance verifyPropertyName:(NSString *)verifyPropertyName
{
    unsigned int outCount, i;
    
    // 获取对象里的属性列表
    objc_property_t * properties = class_copyPropertyList([instance
                                                           class], &outCount);
    
    for (i = 0; i < outCount; i++) {
        objc_property_t property =properties[i];
        //  属性名转成字符串
        NSString *propertyName = [[NSString alloc] initWithCString:property_getName(property) encoding:NSUTF8StringEncoding];
        // 判断该属性是否存在
        if ([propertyName isEqualToString:verifyPropertyName]) {
            free(properties);
            return YES;
        }
    }
    free(properties);
    
    return NO;
}



@end

参考链接

相关文章

  • iOS开发-项目中减少if-else 的使用

    大概是受够了if-else,特意去网上找了资料,参考了很多资料,现在自己做一个总结。 看了上面的代码很简单吧,现在...

  • 减少if-else的使用

    前言 之前写了很多代码,后来看看,发现里面有很多的if-else的嵌套,代码都是平铺直叙的,即使是很简单的逻辑,用...

  • iOS项目中SDK的开发与调试

    iOS项目中SDK的开发与调试 iOS项目中SDK的开发与调试

  • 线程安全的iOS通用缓存-SwiftlyCache

    iOS开发中或多或少都会使用到Cache来减少网络请求,在网络上也有很多使用Objective-c开发的Cache...

  • 优化过多的if-else

    开发中使用到if-else是再正常不过的了,如果需要判断的条件比较少,使用少量的if-else是最简单不过的,但是...

  • iOS 开发中的泛型

    在iOS开发中,泛型的使用,可以减少沟通的成本,明确类型。相信大家在开发中,或多或少,都接触或使用过泛型。比如在定...

  • 通过Runtime源码了解关联对象的实现

    原文链接 在iOS开发中,Category是经常使用到的一个特性,合理的使用Category能够减少繁琐代码,提高...

  • iOS 制作自己的cocopods私有库

    在iOS开发中,越来越多人使用模块化开发了,使用模块化开发的前提是学会制作私有库,以方便项目中导入使用,之前制作过...

  • RN - Git拉下的项目无法正常运行

    最近在iOS项目中导入RN,学习使用RN混合开发,磕磕绊绊写出了一个页面,然后上传至Git,准备正式使用混合开发。...

  • 记录

    1.使用XXX.pch文件便捷开发+加速Build 在IOS开发的项目中有一个XX_Prefix.pchXX_Pr...

网友评论

      本文标题:iOS开发-项目中减少if-else 的使用

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