First: Methods in runtime.h bool Class_addproperty (class CLS, const char *name,
const objc_property_attribute_t * attributes, unsigned int attributecount)
#include <objc/runtime.h> #import <Foundation/Foundation.h> @interface someclass:nsobject {Nsstri ng *_privatename;}
@end @implementation someclass-(ID) init {self = [super init];
if (self) _privatename = @ "Steve"; return self;} @endNSString *namegetter (id self, SEL _cmd) {Ivar Ivar = class_getinstancevariable ([SomeClass class], "_privatename"
);
Return Object_getivar (self, Ivar); void Namesetter (id self, SEL _cmd, NSString *newname) {Ivar Ivar = class_getinstancevariable ([SomeClass class], "_PR
Ivatename ");
ID oldname = Object_getivar (self, Ivar); if (oldname!= newName) Object_setivar (self, Ivar, [newName copy]);
int main (void) {@autoreleasepool {objc_property_attribute_t type = {"T", "@/" nsstring/"}; objc_property_attribute_t ownership = {"C", ""};
C = Copy objc_property_attribute_t Backingivar = {"V", "_privatename"}; objc_property_attribute_t attrs[] = {type, ownership, backIngivar};
Class_addproperty ([SomeClass class], "name", Attrs, 3);
Class_addmethod ([SomeClass class], @selector (name), (IMP) Namegetter, "@@:");
Class_addmethod ([SomeClass class], @selector (setname:), (IMP) namesetter, "v@:@");
ID o = [someclass new];
NSLog (@ "%@", [o name]);
[O setname:@ "Jobs"];
NSLog (@ "%@", [o name]); Output: Stevejobs The second:-(ID) Valueforundefinedkey: (NSString *) key third: static char const * Const objecttagkey;@ Implementation NSObject (Examplecategorywithproperty) @dynamic objecttag;-(id) objecttag {return Objc_getassociatedo
Bject (self, objecttagkey); }-(void) Setobjecttag: (id) newobjecttag {objc_setassociatedobject (self, objecttagkey, NewObject, Objc_association_ retain_nonatomic);}