class_copyIvarList和class_copyMethodList
来源:互联网 发布:c语言常用数学函数下载 编辑:程序博客网 时间:2024/06/08 03:11
直接看代码吧
Dog.h
#import <Foundation/Foundation.h>@interface Dog : NSObject@property(nonatomic, strong) NSString * dogName;@property(nonatomic, assign) NSInteger dogAge;@end
Dog.m
#import "Dog.h"@interface Dog ()@property(nonatomic, strong) NSString * dogSex;@end@implementation Dog- (instancetype)init{ self = [super init]; if (self) { self.dogName = @"dahuang"; self.dogAge = 2; } return self;}- (void) printDogName{ NSLog(@"dogName");}@end
测试代码:
#import "ViewController.h"#import <objc/runtime.h>#import <objc/message.h>#import "Person.h"#import "Dog.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. Dog * dog = [[Dog alloc] init]; unsigned int outCount = 0; unsigned int outCountMethod = 0; Ivar * ivars = class_copyIvarList([dog class], &outCount); Method * methods = class_copyMethodList([dog class], &outCountMethod); for (int i = 0; i<outCount; i++) { // 取出i位置对应的成员变量 Ivar ivar = ivars[i]; // 查看成员变量 const char *name = ivar_getName(ivar); NSLog(@"%s", name); } for (int j = 0; j < outCountMethod; j++) { Method method = methods[j]; SEL methodSEL = method_getName(method); const char * selName = sel_getName(methodSEL); if (methodSEL) { NSLog(@"sel------%s", selName); } } }
输出:
2015-11-02 12:18:07.851 02-runtime[1970:51391] _dogName2015-11-02 12:18:07.852 02-runtime[1970:51391] _dogAge2015-11-02 12:18:07.853 02-runtime[1970:51391] _dogSex2015-11-02 12:18:07.853 02-runtime[1970:51391] sel------setDogName:2015-11-02 12:18:07.854 02-runtime[1970:51391] sel------setDogAge:2015-11-02 12:18:07.855 02-runtime[1970:51391] sel------printDogName2015-11-02 12:18:07.855 02-runtime[1970:51391] sel------dogName2015-11-02 12:18:07.855 02-runtime[1970:51391] sel------dogAge2015-11-02 12:18:07.856 02-runtime[1970:51391] sel------dogSex2015-11-02 12:18:07.856 02-runtime[1970:51391] sel------setDogSex:2015-11-02 12:18:07.856 02-runtime[1970:51391] sel------.cxx_destruct2015-11-02 12:18:07.857 02-runtime[1970:51391] sel------init
0 0
- class_copyIvarList和class_copyMethodList
- class_copyIvarList和class_copyMethodList实例
- class_copyPropertyList和class_copyIvarList的区别
- class_copyPropertyList与class_copyIvarList区别
- class_copyPropertyList与class_copyIvarList的区别
- iOS runtime ~ class_copyPropertyList与class_copyIvarList区别
- 运行时简单使用-->获取对象的属性(class_copyIvarList)
- class_copyPropertyList与class_copyIvarList区别,获取属性,使用runtime方法的注意事项
- . 和 ->
- || 和&&
- .和::
- “?”和“!”
- #和##
- && 和 ||
- 、和
- #和##
- .//和..//
- 和
- Java中List实现之ArrayList
- 链表排序--选择排序
- 小胖说事50-----IOS 检测APP是否在手机上安装并且打开APP
- 详解C++中命名空间的意义和用法
- hdu5521 Meeting (最短路)
- class_copyIvarList和class_copyMethodList
- 背景透明,文字不透明全兼容方案
- iOS 集成环信(三) 添加好友
- 链表排序--快速排序
- 欢迎使用CSDN-markdown编辑器
- 显示 metadata lock 信息的 mysql 插件
- NEUOJ1402(矩阵快速幂)
- git避免每次提交输入密码
- Caused by: java.lang.ClassNotFoundException[android的终极解决错误]