iOS运行时获取对象的成员变量和成员方法
2014-01-28 14:20
381 查看
1、包含运行时头文件
#import <objc/runtime.h>
2、获取某个类的成员变量或则属性;
unsigned
int numIvars;
//成员变量个数
Ivar *vars =
class_copyIvarList(NSClassFromString(@"UIView"), &numIvars);
//Ivar *vars = class_copyIvarList([UIView class], &numIvars);
NSString *key=nil;
for(int i =
0; i < numIvars; i++) {
Ivar thisIvar = vars[i];
key = [NSString
stringWithUTF8String:ivar_getName(thisIvar)];
//获取成员变量的名字
NSLog(@"variable name :%@", key);
key = [NSString
stringWithUTF8String:ivar_getTypeEncoding(thisIvar)];
//获取成员变量的数据类型
NSLog(@"variable type :%@", key);
}
free(vars);
3、获取成员函数
Method *meth =
class_copyMethodList(NSClassFromString(@"UIView"), &numIvars);
//Method *meth = class_copyMethodList([UIView class], &numIvars);
for(int i =
0; i < numIvars; i++) {
Method thisIvar = meth[i];
SEL sel = method_getName(thisIvar);
const char *name =
sel_getName(sel);
NSLog(@"zp method :%s", name);
}
free(meth);
例子中使用的UIView类可以替换成任何自定义的类。更多函数查看<objc/runtime.h> !
#import <objc/runtime.h>
2、获取某个类的成员变量或则属性;
unsigned
int numIvars;
//成员变量个数
Ivar *vars =
class_copyIvarList(NSClassFromString(@"UIView"), &numIvars);
//Ivar *vars = class_copyIvarList([UIView class], &numIvars);
NSString *key=nil;
for(int i =
0; i < numIvars; i++) {
Ivar thisIvar = vars[i];
key = [NSString
stringWithUTF8String:ivar_getName(thisIvar)];
//获取成员变量的名字
NSLog(@"variable name :%@", key);
key = [NSString
stringWithUTF8String:ivar_getTypeEncoding(thisIvar)];
//获取成员变量的数据类型
NSLog(@"variable type :%@", key);
}
free(vars);
3、获取成员函数
Method *meth =
class_copyMethodList(NSClassFromString(@"UIView"), &numIvars);
//Method *meth = class_copyMethodList([UIView class], &numIvars);
for(int i =
0; i < numIvars; i++) {
Method thisIvar = meth[i];
SEL sel = method_getName(thisIvar);
const char *name =
sel_getName(sel);
NSLog(@"zp method :%s", name);
}
free(meth);
例子中使用的UIView类可以替换成任何自定义的类。更多函数查看<objc/runtime.h> !
相关文章推荐
- iOS Xcode工程目录的 folder 和 group的区别(蓝色和黄色文件夹的区别)
- 在iOS使用ZipArchive压缩和解压缩文件
- Afaria 支持supervised mode for iOS设备
- iOS开发:XIB使用技巧总结
- iOS多线程系列(1)
- iOS多线程系列(1)
- IOS 常用cache
- BIOS基础知识(下)
- BIOS基础知识(上)
- iOS category中的所谓属性 和 从xib初始化对象的方法 以及类扩展
- ios应用在不同语言及区域的日期显示
- 疯狂ios讲义疯狂连载之显示动画
- 疯狂ios讲义疯狂连载之显示动画
- iOS开发-retain/assign/strong/weak/copy/mutablecopy/autorelease区别
- ane教程之IOS
- iOS 定位 基站 cellid
- ios 模拟点击
- 解决IOS无法播放3gp视频, 出现无法编码问题
- 在iOS上实现二维码功能
- TTY termios 结构体