一般情况下,oc中分类只可以新增方法,不可以新增属性。但是通过关联可以间接实现新增属性.
#import
@classQDFStatisticsEvent;
@interfaceUIView (QDFStatistics)
@property(nonatomic,strong)QDFStatisticsEvent*qdfEvent;
@end
#import"UIView+QDFStatistics.h"
#import
#import"QDFStatisticsEvent.h"
@implementationUIView (QDFStatistics)
static char s_qdf_control_key;
//给属性赋值
- (void)setQdfEvent:(QDFStatisticsEvent*)qdfEvent
{
objc_setAssociatedObject(self, &s_qdf_control_key,qdfEvent,OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
//获取属性值
- (QDFStatisticsEvent*)qdfEvent
{
returnobjc_getAssociatedObject(self,&s_qdf_control_key);//需要包含#import
}
@end