ios 注解 (参照android 注解)

经常听android的同事说注解,说这个技术真好用。好奇心之下,我也搜索了下ios的注解。完蛋,ios的注解大多数都是讲怎么注释的,根本没有注解。我就在想我们ios上能不能也实现注解这个功能呢?想实现这个功能,我们必须要懂得注解是个什么东西才行,因此我就询问同事和网上查资料,弄懂了注解到底是个啥东西。

注解

android 注解的解释

注解(Annotaion),也叫元数据。一种代码级别的说明。它是JDK1.5及以后版本引入的一特性,与类、接口、枚举是在同一个层次。它可以声明在包、类、字段、方法、局部变量、方法参数等的前面,用来对这些元素进行说明、注解。

在我看来注解就是一个标签。用来标示类、字段、方法等的特性。

注解分类

根据声明周期,android的注解分为三类

  • 源代码注解
  • 编译时注解
  • 运行时注解

源代码注解

主要作用是编译检查。

在编译代码之前就让编译器给你查阅代码的合法性,相当于ios上的类型检查。这个注解在编译阶段就没用了。
这个暂时没想到办法能实现帮助编译器做校验。

编译时注解

在编译阶段让编译器主动帮助我们生成一些类,而不用我们自己编写这些类,我们只要告诉编译器如何编译这个类就行了。这个注解在编译结束后已经生成了我们所需要的代码。

运行时注解

在运行时,我们可以获取类,方法,属性等我们给其打的注解(标签),这个在程序启动的时候帮助我们绑定类,方法,属性。

以上是个人理解。不对的请各路大神指正。

从上面这几种注解中,源代码注解,我目前是没有一点想实现的想法(无从下手)。编译时注解,是在程序编译阶段帮助我们自动生成一些文件添加到工程里面,这个需要编译器去做,貌似也做不了。能力所限,剩下的只能做个运行时注解了。

运行时注解

场景一:我们创建TableView, 有三项,每项都应一个vc跳转,如果我们新增加一项或者删除一项,我们就需要修改TableView,所在文件的代码。
如下

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) NSMutableArray  *datasource;
@end


@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
   UITableView * table= [[UITableView alloc]initWithFrame: [UIScreen mainScreen].bounds style:0];
    table.delegate = self;
    table.dataSource = self;
    self.datasource = [NSMutableArray array];
    [table registerClass:[UITableViewCell class] forCellReuseIdentifier:@"vc"];
    [self.datasource addObject:[[OPFirstViewController alloc]init]];
    [self.datasource addObject:[[OPTwoViewController alloc]init]];
     [self.datasource addObject:[[OPThirdViewController alloc]init]];
    [self.view addSubview:table];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"vc"];
    if (cell) {
        cell.textLabel.text = NSStringFromClass([[self.datasource objectAtIndex:indexPath.row] class]);
    }
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.datasource.count;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self presentViewController:[self.datasource objectAtIndex:indexPath.row] animated:YES completion:^{
        
    }];
}
image.png

如果我们需要再新增一个类** OPFourViewController**,我们需要改动代码如下

#import "OPFourViewController.h"
 [self.datasource addObject:[[OPFourViewController alloc]init]];
image.png

上面的写法肯定没有问题。

那用运行时注解怎么做呢?
我们创建一个注解类

#import "OPBaseAnnotation.h"

@interface OPVCAnnotion : OPBaseAnnotation
@property (nonatomic,strong) NSString *type;
@property (nonatomic,strong) NSString *name;
+(NSSet *)getVCWithType:(NSString *)type;
@end

#import "OPVCAnnotion.h"
static NSMutableDictionary * vcDic;

@implementation OPVCAnnotion

- (instancetype)init
{
    self = [super init];
    if (self) {
    }
    return self;
}
-(OPAnnotationQualifiedType)getAnnotationQualifiedType{
    return OPAnnotationQualifiedClass;
}

+(NSSet *)getVCWithType:(NSString *)type{
    if (!vcDic) {
        return nil;
    }
    NSDictionary * dicName = [vcDic objectForKey:@"type"];
    
    if (dicName) {
        return  [dicName objectForKey:type];
    }
    return nil;
}

-(OPBaseAnnotation *(^)(id))build{
    return ^OPBaseAnnotation*(id object){
        if (!self.type) {
            return self;
        }
        if (!vcDic) {
            vcDic = [NSMutableDictionary dictionary];
        }
        //       self.name
        NSMutableDictionary * clsDic = [vcDic   objectForKey:@"type"];
        if (!clsDic) {
            clsDic = [NSMutableDictionary new];
            [vcDic setObject:clsDic forKey:@"type"];
        }
        
        NSMutableSet * set = [clsDic objectForKey:self.type];
        if (!set) {
            set = [NSMutableSet new];
            [clsDic setObject:set forKey:self.type];
        }
        [set addObject:object];
        
        return self;
    };
}

@end

我们在每个类的.m 文件中加入我们需要的注解

#import "OPFirstViewController.h"

#import "OPHead.h"
#import "OPVCAnnotion.h"
OPClassAnnotation(OPFirstViewController, OPVCAnnotion,@"type=UI",@"name=firstVC");

@interface OPFirstViewController ()

@end

@implementation OPFirstViewController

- (void)viewDidLoad {
    [super viewDidLoad];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

调用

   NSSet * set =  [OPVCAnnotion getVCWithType:@"UI"];
    for (Class cls in set) {
        [self.datasource addObject:[[cls.class alloc]init]];
    }
    

ok 运行项目


image.png

我们发现,我们再也不用关心TableView的 dataSource
只要是我们给vc 添加了注解,我们就能从OPVCAnnotion中获取到。

运行时注解实现

我们知道注解相当于个标签,标签可以绑定类,属性或者方法。在运行的时候运行一段代码,因此这段代码只能在.m文件中了(.h文件不能有函数实现)。因此我们需要考虑的怎么在运行时将类,属性或者方法和标签进行绑定,并且是在程序运行的开始阶段就绑定。

第一种方案:是在+load 方法进行绑定。
第二种方案:是在attribute ((constructor)) 标记的方法中绑定

这两种方案都是在程序启动,调用main函数之前调用的,我抛弃了第一种方案,因为+load 方法,大家有时候需要用的嘛。第二种方案随便命名,只要用attribute ((constructor)) 标记就可以了。

因为需要类和注解之间绑定关系,我们这里采用单例类来记录类和注解之间的绑定关系。

关系图

image.png
@interface OPAnnotationManager : NSObject
+(OPAnnotationManager *)shareOPAnnotationManager;
///类注解

///增加一个类注解
-(void)addClass:(Class)cls AnnotationCls:(Class)annotation annotationParams:(NSDictionary *)params;

@interface OPAnnotationManager()
@property (nonatomic,strong) NSMutableDictionary *annationClsDic;
@end
@implementation OPAnnotationManager
static OPAnnotationManager * manager = nil;
+(OPAnnotationManager *)shareOPAnnotationManager{
    static dispatch_once_t onceToken = 0;
    dispatch_once(&onceToken, ^{
        manager = [[OPAnnotationManager alloc]init];
    });
    return manager;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.annationClsDic = [NSMutableDictionary dictionary];
    }
    return self;
}

#pragma mark - 类方法注解
-(void)addClass:(Class)cls AnnotationCls:(Class)annotation annotationParams:(NSDictionary *)params{
    NSString * clsStr =   NSStringFromClass(cls);
    NSMutableSet * set = [self.annationClsDic objectForKey:clsStr];
    if (!set) {
        set = [NSMutableSet set];
        [self.annationClsDic setObject:set forKey:clsStr];
    }
    Class annotationCls = annotation;
  ///如何实现 检查类型
    
    OPBaseAnnotation* ann=  [[annotationCls.class alloc]init];
    if (!((ann.getAnnotationQualifiedType & OPAnnotationQualifiedClass)==OPAnnotationQualifiedClass)) {
        OPLog(@"OPAnnotation:   Annation %@ not qualifiedClass %@",annotation,clsStr);

        return;
    }
    for (NSString * key in params) {
        @try {
            [ann setValue:params[key] forKey:key];
        } @catch (NSException *exception) {
            OPLog(@"OPAnnotation: 类 %@  Annation %@ not key %@",clsStr,annotation,key);
        } @finally {
            
        }    }
    if (ann.build){
        ann.build(cls);
    }
   
    [set addObject:ann];
}


typedef enum : NSUInteger {
    OPAnnotationQualifiedNone=0,
    OPAnnotationQualifiedClass=1<<1,
    OPAnnotationQualifiedProperty=1<<2,
    OPAnnotationQualifiedMethod=1<<3,
    OPAnnotationQualifiedMask=0x00FF,
} OPAnnotationQualifiedType;

@interface OPBaseAnnotation : NSObject
-(OPBaseAnnotation*(^)(id object))build;
-(OPAnnotationQualifiedType)getAnnotationQualifiedType;
@end

#import "OPBaseAnnotation.h"

@implementation OPBaseAnnotation
-(OPAnnotationQualifiedType)getAnnotationQualifiedType{
    return OPAnnotationQualifiedNone;
}

-(OPBaseAnnotation*(^)(id object))build{
    return ^(id object){
        return self;
    };
}



@end

上述是单例类的实现,这里我们还创建了个Annotation类,就是为了图中的那条虚线,我们可以通过类找到注解,我们也应该可以通过注解找到相关的类,我们将类给注解类,具体如何处理是我们自己的事情了。

如何添加注解呢?因为可能有多个参数传入,因此我们这里采用宏定义的方式进行处理

///类注解
#define  OPClassAnnotation(ClassA,AnnotionClassA,...) __OPClassAnnotation(ClassA,AnnotionClassA,##__VA_ARGS__,10,9,8,7,6,5,4,3,2,1,0)

#define __OPClassAnnotation(ClassA,AnnotionClassA,_1, _2,_3,_4,_5,_6,_7,_8,_9,_10, N, ...) \
void __attribute__ ((constructor)) OPclassAnnotation##ClassA##AnnotionClassA##func(){  \
NSDictionary *params=nil;\
if (N==0){\
}else{\
params =__OPAnnotationParams(_1,_2,_3,_4,_5,_6,_7,_8,_9,_10,N);\
}\
[[OPAnnotationManager shareOPAnnotationManager] addClass:ClassA.class AnnotationCls:AnnotionClassA.class annotationParams:params];\
}

获取不定参数

NSDictionary  * __OPGetAnnotationParamsDic(NSUInteger count, ...) {
    NSMutableDictionary *paramDic = [NSMutableDictionary new];
    va_list args;
    va_start(args, count);
    for (NSUInteger x = 0; x < count; ++x){
        NSString * paramStr  = va_arg(args, id);
        NSArray * paramArr =  [paramStr componentsSeparatedByString:@"="];
        if (paramArr.count==2) {
            [paramDic setObject:paramArr[1] forKey:paramArr[0]];
        }
    }
    va_end(args);
    return paramDic;
}

因为实现起来不难,这里只是抛砖引玉,希望各种大牛真正实现ios的注解。

源码实现了属性 ,类和类方法的注解。

源码地址

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,390评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,821评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,632评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,170评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,033评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,098评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,511评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,204评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,479评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,572评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,341评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,893评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,171评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,486评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,676评论 2 335

推荐阅读更多精彩内容

  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    X先生_未知数的X阅读 15,960评论 3 119
  • 我一直想不明白两个问题:1.男人对红颜知己比对女朋友好 2.女人天天找朋友说爱情不好,但是爱情永远比友情重要 木木...
    你好曹公公阅读 239评论 0 0
  • iOS 低版本适配,真机测试所用 device support iOS 6 device support 链接: ...
    ReidWang阅读 440评论 0 0
  • 今天从送完孩子回来就一直下雨。现在还下着。 早上没有吃饭,不光不饿,而且一直想吐。胃里以前不舒服的一个地方,一直有...
    慧敏王阅读 321评论 1 3
  • 首先这部电影我给一颗星特效,然后,没有了。 特效做得很漂,特别是爱丽丝开着超时空魔球,穿越时间之海层层巨浪回到过去...
    edenvicky阅读 115评论 0 0