一般情況下,oc中分類只可以新增方法纷闺,不可以新增屬性算凿。但是通過關(guān)聯(lián)可以間接實(shí)現(xiàn)新增屬性.
#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