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

不同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开发了.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,138评论 25 709
  • 前言: 本文转自前同事casa的博文,这篇文章是基于runtime实现的iOS组件化方案,其实iOS组件化方案基本...
    monkey01阅读 1,691评论 1 2
  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,241评论 30 472
  • 他侧身坐着,与往常一样,铮亮的漆黑军靴有一下无一下的踏着地面,似乎有些不耐烦,又似乎是消磨时间的动作。他还是...
    莫思停阅读 594评论 0 4
  • “无论在什么地方,我都在关注着你们,你们是我最熟悉的一帮学生,也永远是我最爱的一帮孩子们……”这是出自教我们...
    静候花语阅读 427评论 5 4