这里举个简单的例子来介绍一下如何动态创建类(Student):
const char * className;
className = [@"Student" UTF8String];
Class kclass = objc_getClass(className);
//判断此类是否已经存在,如果存在则返回,不存在就创建
if (!kclass)
{
Class superClass = [NSObject class];
kclass = objc_allocateClassPair(superClass, className, 0);
}
else return;
为Student添加一个NSString类型的成员变量stuName
//为类添加成员变量
class_addIvar(kclass, "_stuName", sizeof(NSString *), 0, "@");
为Student添加一个say:方法
这里第一个参数为类名,第二个参数为方法名,第三个参数是函数名,第四个参数是函数的返回值和参数的类型,v表是void,@表示id,:表示SEL,定义参考
//为类添加方法
class_addMethod([kclass class], @selector(say:), (IMP)say, "v@:");
需要实现这个方法
//这个方法实际上没有被调用,但是必须实现否则不会调用下面的函数
- (void)say:(NSString *)aString
{
}
//self和_cmd是必须的,在之后可以随意添加其他参数
void say(id self,SEL _cmd,NSString *aString)
{
NSLog(@"你好%@",aString);
}
为Student添加一个stuSex属性
NSString *propertyName = @"stuSex";
objc_property_attribute_t type = { "T", "@\"NSString\"" };
objc_property_attribute_t ownership = { "C", "copy" };
objc_property_attribute_t backingivar = { "V", [propertyName UTF8String]};
objc_property_attribute_t attrs[] = { type, ownership, backingivar };
BOOL isOk=class_addProperty(kclass, [propertyName UTF8String], attrs, 3);
然后注册这个类
objc_registerClassPair(kclass);
然后调用一下试试
id p = [[kclass alloc] init];
[p setValue:@"张三" forKey:@"stuName"];
[p setValue:@"男" forKey:@"stuSex"];
NSLog(@"%@",[p valueForKey:@"stuSex"]);
NSLog(@"%@",[p valueForKey:@"stuName"]);
[p say:[p valueForKey:@"stuName"]];
此时程序是会出错的。因为此时属性是不可以调用setValue:forKey:方法的,为此我在网上找了很多资料,大部分都是说需要自己去添加方法如下:
class_addMethod(kclass,@selector(stuSex), (IMP)Getter, "@@:");
class_addMethod(kclass,@selector(setStuSex:), (IMP)Setter, "v@:@");
//在创建类的时候再添加上面两个方法。然后去实现这两个方法
- (void)setStuSex:(NSString *)stuSex
{
}
- (NSString *)stuSex
{
return nil;
}
NSString * Getter(id self1,SEL _cmd1){
NSString * var=NSStringFromSelector(_cmd1);
Ivar ivar=class_getInstanceVariable([self1 class], [var cStringUsingEncoding:NSUTF8StringEncoding]);
return object_getIvar(self1, ivar);
}
void Setter(id self1,SEL _cmd1,NSString* newObject){
NSString * var=[NSStringFromSelector(_cmd1) stringByReplacingCharactersInRange:NSMakeRange(0, 3) withString:@""];
NSString * head=[var substringWithRange:NSMakeRange(0, 1)];
head=[head lowercaseString];
var=[var stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:head];
var=[var stringByReplacingCharactersInRange:NSMakeRange([var length]-1, 1) withString:@""];
Ivar ivar=class_getInstanceVariable([self1 class], [var cStringUsingEncoding:NSUTF8StringEncoding]);
id oldName=object_getIvar(self1, ivar);
if (oldName!=newObject) {
object_setIvar(self1, ivar, [newObject copy]);
}
}
然后去调用这两个方法以达到赋值和取值的目的
[p setStuSex:@"男"];
NSLog(@"%@",[p stuSex]);
结果是空
那么该如何解决这个问题呢。其实是因为在添加属性的时候需要关联一个成员变量,所以我们需要再去声明这个成员变量,并在添加属性的时候去关联它:
NSString *propertyName = @"stuSex";
//在这里添加这么一句就可以了
class_addIvar(kclass, [propertyName UTF8String], sizeof(NSString *), 0, "@");
objc_property_attribute_t type = { "T", "@\"NSString\"" };
objc_property_attribute_t ownership = { "C", "copy" };
objc_property_attribute_t backingivar = { "V", [propertyName UTF8String]};
objc_property_attribute_t attrs[] = { type, ownership, backingivar };
BOOL isOk=class_addProperty(kclass, [propertyName UTF8String], attrs, 3);
结果显示如下:
如果在添加属性的时候先添加一个成员变量并进行关联,此时我们是不需要再去添加别的方法也可以使用setValue:forKey:的,所以说在添加属性时是必需要关联到一个成员变量上的,结合iOS反射机制: objc_property_t的使用可知我们在使用@property时应该是已经帮我们关联了一个成员变量。
所有代码如下:
- (void)createClass
{
const char * className;
className = [@"Student" UTF8String];
Class kclass = objc_getClass(className);
//判断此类是否已经存在,如果存在则返回,不存在就创建
if (!kclass)
{
Class superClass = [NSObject class];
kclass = objc_allocateClassPair(superClass, className, 0);
}
else return;
//为类添加成员变量
class_addIvar(kclass, "_stuName", sizeof(NSString *), 0, "@");
//为类添加方法
class_addMethod([kclass class], @selector(say:), (IMP)say, "v@:");
NSString *propertyName = @"stuSex";
class_addIvar(kclass, [propertyName UTF8String], sizeof(NSString *), 0, "@");
objc_property_attribute_t type = { "T", "@\"NSString\"" };
objc_property_attribute_t ownership = { "C", "copy" };
objc_property_attribute_t backingivar = { "V", [propertyName UTF8String]};
objc_property_attribute_t attrs[] = { type, ownership, backingivar };
BOOL isOk=class_addProperty(kclass, [propertyName UTF8String], attrs, 3);
// class_addMethod(kclass,@selector(stuSex), (IMP)Getter, "@@:");
// class_addMethod(kclass,@selector(setStuSex:), (IMP)Setter, "v@:@");
//
NSLog(@"add property =%d",isOk);
objc_registerClassPair(kclass);
id p = [[kclass alloc] init];
[p setStuSex:@"男"];
NSLog(@"%@",[p stuSex]);
[p setValue:@"张三" forKey:@"stuName"];
[p setValue:@"男" forKey:@"stuSex"];
NSLog(@"%@",[p valueForKey:@"stuSex"]);
NSLog(@"%@",[p valueForKey:@"stuName"]);
[p say:[p valueForKey:@"stuName"]];
NSLog(@"%@",[p valueForKey:@"stuSex"]);
}
- (void)setStuSex:(NSString *)stuSex
{
}
- (NSString *)stuSex
{
return nil;
}
//这个方法实际上没有被调用,但是必须实现否则不会调用下面的方法
- (void)say:(NSString *)aString
{
}
//self和_cmd是必须的,在之后可以随意添加其他参数
void say(id self,SEL _cmd,NSString *aString)
{
NSLog(@"你好%@",aString);
}
//NSString * Getter(id self1,SEL _cmd1){
// NSString * var=NSStringFromSelector(_cmd1);
// Ivar ivar=class_getInstanceVariable([self1 class], [var cStringUsingEncoding:NSUTF8StringEncoding]);
// return object_getIvar(self1, ivar);
//}
//
//void Setter(id self1,SEL _cmd1,NSString* newObject){
// NSString * var=[NSStringFromSelector(_cmd1) stringByReplacingCharactersInRange:NSMakeRange(0, 3) withString:@""];
// NSString * head=[var substringWithRange:NSMakeRange(0, 1)];
// head=[head lowercaseString];
// var=[var stringByReplacingCharactersInRange:NSMakeRange(0, 1) withString:head];
// var=[var stringByReplacingCharactersInRange:NSMakeRange([var length]-1, 1) withString:@""];
// Ivar ivar=class_getInstanceVariable([self1 class], [var cStringUsingEncoding:NSUTF8StringEncoding]);
// id oldName=object_getIvar(self1, ivar);
// if (oldName!=newObject) {
// object_setIvar(self1, ivar, [newObject copy]);
// }
//}