美文网首页iOS
iOS 项目多target中file和code建议方案

iOS 项目多target中file和code建议方案

作者: PittWong | 来源:发表于2017-02-07 11:51 被阅读53次

    不同target中引用同名文件

    在多target项目中可以通过不同文件选择不同target,从而在编译过程中选择包含不同的文件.

    1.简单粗暴copy文件

    这就不用多说了,就是完全复制代码,在新文件中根据需求更改对应代码,从而对原文件不产生干扰,但与原文件存在大量重复代码,日后维护成本非常高,不建议采用此种方式.(也正是看到很多同事再用这种方式码代码才有整理建议方案的想法)

    2.在单个文件中做各种判断

    这样会把原本就已是复杂逻辑的文件中加入各种target的代码,从而导致文件异常庞大,加大了后续维护成本.

    3.通过category方式

    首先要知道以下4个iOS 的特点(面向对象都差不多)

    1.category与原类型实现同名方法,运行时只走category中方法
    2.在category中调用category中没有的方法,运行时回去找原类型中此方法,若原类型中也没有实现,则追找父类方法实现.
    3.原类型.m文件中的属性,可通过protocol方式绕道在category中调用(黑魔法).
    4.运行时动态交换方法a和b,代码调用a时执行b;

    a.通过protocol方式,将在category要用到的属性和方法进行声明,方便在category中可直接调用

    @protocol XXTargetProtocol <NSObject>
    
    @optional
    @property (nonatomic , weak ) UILabel *label;    //category中需要用到的原文件中的属性
    - (void)targetBaseMethod;                        //原文件中需要被替换的方法
    
    @end
    
    @interface ViewController (targetB)< XXTargetProtocol >
    
    @end
    

    b.确定2个target差异,在targetBMethod和targetBaseMethod中分别实现

    - (void)targetBaseMethod {
         self.label.text = @"base";
        self.label.frame = CGRectMake(100, 100, 100, 30);
    }
    
    - (void)targetBMethod {
        self.label.text = @"B";
        self.label.frame = CGRectMake(100, 300, 100, 30);
    }
    

    c.在category合适的方法中交换targetB和base中的方法(常用init或loadView方法中二选其一使用)

    - (void)loadView {
        [super loadView];
        //交换需要特殊设置的方法
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            Method method0 = class_getInstanceMethod([self class], @selector(targetBaseMethod));
            Method method1 = class_getInstanceMethod([self class], @selector(targetBMethod));
            method_exchangeImplementations(method0, method1);
        });
    }
    
    - (instancetype)init {
        self = [super init];
        if (self) {
            //交换需要特殊设置的方法
            static dispatch_once_t onceToken;
            dispatch_once(&onceToken, ^{
                Method method0 = class_getInstanceMethod([self class], @selector(targetBaseMethod));
                Method method1 = class_getInstanceMethod([self class], @selector(targetBMethod));
                method_exchangeImplementations(method0, method1);
            });
        }
        return self;
    }
    

    大功告成,这样在原文件中仅需要在各个target有差异地方抽取对应base方法,每个target对应一个category实现各自特殊方法便可轻松搞定多target开发了.

    相关文章

      网友评论

        本文标题:iOS 项目多target中file和code建议方案

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