Category动态增加属性

.h

import

@interface UISwitch (WithKey)

@property (nonatomic,strong)NSString *keyString;

@end

.m

import "UISwitch+WithKey.h"

import

@implementation UISwitch (WithKey)

static char keyStringKey;

-(NSString *)keyString
{
return objc_getAssociatedObject(self, &keyStringKey);
}

-(void)setKeyString:(NSString *)keyString
{
objc_setAssociatedObject(self, &keyStringKey, keyString, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

@end

@property (nonatomic, assign) NSInteger subRow;

pragma mark - NSIndexPath (SKSTableView)

static void *SubRowObjectKey;

@implementation NSIndexPath (SKSTableView)

@dynamic subRow;

  • (NSInteger)subRow
    {
    id subRowObj = objc_getAssociatedObject(self, SubRowObjectKey);
    return [subRowObj integerValue];
    }

  • (void)setSubRow:(NSInteger)subRow
    {
    id subRowObj = [NSNumber numberWithInteger:subRow];
    objc_setAssociatedObject(self, SubRowObjectKey, subRowObj, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }

你可能感兴趣的:(Category动态增加属性)