`
jsntghf
  • 浏览: 2473586 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

如何给category添加属性

    博客分类:
  • iOS
阅读更多

主要是使用了runtime中的associative机制。

 

NSDate+extension.h

@interface NSDate (extension)

@property (nonatomic, strong) NSDateFormatter *formatter;

@end

 

NSDate+extension.m

#import "NSDate+extension.h"
#include <objc/runtime.h>

static void *formatterKey = (void *) @"formatterKey";

@implementation NSDate (extension)
@dynamic formatter;

+ (NSDateFormatter *)formatter {
	return objc_getAssociatedObject(self, formatterKey);
}

+ (void)setFormatter:(NSDateFormatter *)formatterProperty {
	objc_setAssociatedObject(self, formatterKey, formatterProperty, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

@end

 

分享到:
评论
1 楼 alec03711 2015-08-26  
还写OC代码呢,都开始用Swift啦

相关推荐

Global site tag (gtag.js) - Google Analytics