使用 #import <objc/message.h>
Project -> Targets -> Your Target -> Build Setting -> Search For `objc_msgSend` -> Set `No` (Deafualt is `YES`)
直接通过类名类获取类
id cls = objc_getClass("Person");
// 不需要使用 #import “Person.h”
直接通过类名类获取类的方法表
Ivar *members = class_copyIvarList(objc_getClass("Person"), &count);
// 遍历方法
for (int i = 0; i < count; i++) {
Ivar var = members[i];
const char *memberAddress = ivar_getName(var);
const char *memberType = ivar_getTypeEncoding(var);
NSLog(@"Property[%d] = %s ; type = %s", count, memberAddress, memberType);
}
私有变量的更改
id cls = objc_getClass("Person");
Ivar m_address = members[1];
object_setIvar(cls, m_address, @"朝阳公园");
调用方法
id p = objc_msgSend(objc_getClass("Father"), sel_registerName("alloc"));
objc_msgSend(p, sel_registerName("init"));
NSLog(@"p = %@", p);
objc_msgSend(p, sel_registerName("test3:second:intVaule:"), @"xxx", @"dfjsaf", 123);
网友评论