#import @interface NSObject (Category) - (void)myMethod; @end
#import static const void * externVariableKey =&externVariableKey; @implementation NSObject (Category) @dynamic variable; - (id) variable { return objc_getAssociatedObject(self, externVariableKey); } - (void)setVariable:(id) variable { objc_setAssociatedObject(self, externVariableKey, variable, OBJC_ASSOCIATION_RETAIN_NONATOMIC); }
-----------------------------------------------------------
Extension非常像是没有命名的类别,即匿名类别
@interface MyClass : NSObject @property (retain, readonly) float value; @end //一般的时候,Extension都是放在.m文件中@implementation的上方。 @interface MyClass () @property (retain, readwrite) float value; @end
两者有一点区别
图片摘自: http://blog.sina.com.cn/s/blog_a2c098b50101gsn0.html
iOS类别(Category)与扩展(Extension,匿名类别)
原文地址:http://blog.csdn.net/hherima/article/details/44242469