创建Category;
创建Objective-C file文件,下图File为类别名,Class为需要添加属性的原生类
以UITextField为例;
.h文件
@interfaceUITextField (IndexPath)
@property (nonatomic,strong) NSIndexPath *indexPath;//增加indexPath属性
@end
.m文件
#import "UITextField+IndexPath.h"
#import
@implementationUITextField (IndexPath)
staticcharindexPathKey;
- (NSIndexPath*)indexPath{
return objc_getAssociatedObject(self, &indexPathKey);
}
- (void)setIndexPath:(NSIndexPath*)indexPath{
objc_setAssociatedObject(self, &indexPathKey, indexPath,OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
@end