Edited at

カテゴリでプロパティを追加する

More than 5 years have passed since last update.

#include <objc/runtime.h>

@interface NSObject (AddProperty)
@property (copy) NSString *string;
@end

@implementation NSObject(AddProperty)
- (NSString *)string
{
id result = objc_getAssociatedObject(self, @"string");
return [[result copy] autorelease];
}
- (void)setString:(NSString *)string
{
objc_setAssociatedObject(self, @"string", string, OBJC_ASSOCIATION_COPY);
}
@end