カテゴリでプロパティを追加する
という話
普通にやっても無理なので、Associated Objectを使います。
#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]; } - (void)setString:(NSString *)string { objc_setAssociatedObject(self, @"string", string, OBJC_ASSOCIATION_COPY); } @end
追記:
http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/ObjectiveC/Chapters/ocAssociativeReferences.html#//apple_ref/doc/uid/TP30001163-CH24-SW3
後始末はランタイムがやってくれるそうです!