LoginSignup
16

More than 5 years have passed since last update.

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

Last updated at Posted at 2012-03-23
#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

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16