iOS的一些总结

2021-09-07  本文已影响0人  程序员进阶

目录

1、Aspects框架(AOP面向切面编程)

2、利用关联对象的方式为类别动态添加属性


1、ASpect(AOP面向切面编程)

最重要的两个方法,底层原理是swizzling和消息转发(iOS AOP框架Aspects实现原理),iOS-Aspects框架详解

hook制定的对象方法,

/// Adds a block of code before/instead/after the current `selector` for a specific class.

+ (id<AspectToken>)aspect_hookSelector:(SEL)selector

                          withOptions:(AspectOptions)options

                            usingBlock:(id)block

                                error:(NSError **)error;

- (id<AspectToken>)aspect_hookSelector:(SEL)selector

                          withOptions:(AspectOptions)options

                            usingBlock:(id)block

                                error:(NSError **)error;


2、利用关联对象的方式为类别动态添加属性

.h:
@property (nonatomic, assign) BOOL isCatagoryCommom;

@property (nonatomic, copy) NSString *isTop;

.m

-(void)setIsCatagoryCommom:(BOOL)isCatagoryCommom{

    objc_setAssociatedObject(self,@selector(isCatagoryCommom), [NSNumber numberWithBool:isCatagoryCommom], OBJC_ASSOCIATION_ASSIGN);

}

-(BOOL)isCatagoryCommom{

    returnobjc_getAssociatedObject(self,_cmd);

}

-(void)setIsTop:(NSString *)isTop {

    objc_setAssociatedObject(self,@selector(isTop), isTop, OBJC_ASSOCIATION_COPY_NONATOMIC);

}

-(NSString *)isTop{

    returnobjc_getAssociatedObject(self,_cmd);

}

上一篇下一篇

猜你喜欢

热点阅读