Using OC Runtime
In. h
@interface NSObject (Laserunicorn) @property (nonatomic, strong) Laserunicorn *laserunicorn; @end
In. m
#import <objc/runtime.h>static void * Laserunicornpropertykey = &LaserUnicornPropertyKey; @implementation NSObject (Laserunicorn)-(Laserunicorn *) Laserunicorn { return objc_getassociatedobject (self, Laserunicornpropertykey);} -(void) Setlaserunicorn: (Laserunicorn *) Unicorn { objc_setassociatedobject (self, Laserunicornpropertykey, Unicorn, objc_association_retain_nonatomic); } @end
A more concise way to try @selector (Nameofgetter)
-(Laserunicorn *) Laserunicorn { return objc_getassociatedobject (self, @selector (Laserunicorn));} -(void) Setlaserunicorn: (Laserunicorn *) Unicorn { objc_setassociatedobject (self, @selector (Laserunicorn), Unicorn, objc_association_retain_nonatomic); }
How OC category injects new properties