目录
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);
}
网友评论