关于runtime的一点理解代码
2016-03-14 00:00
447 查看
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
@interface MyClass : NSObject
@end
@implementation MyClass
@end
void myMethodIMP(id self,SEL _cmd){
NSLog(@"runtime hello");
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
NSLog(@"Hello, World!");
NSLog(@"%s",@selector(test));
NSLog(@"%lu",strlen(@selector(test)));
char *s = calloc(100, sizeof(char));
strcat(s,@selector(test));
strcat(s,@selector(test));
NSLog(@"%s",s);
free(s);
class_addMethod([MyClass class], @selector(myMethod), (IMP)myMethodIMP, "v@:");
MyClass *myObject = [[MyClass alloc] init];
[myObject performSelector:@selector(myMethod)];
}
return 0;
}
运行结果
2016-03-14 15:25:44.207 KKBOX[5786:187150] Hello, World!
2016-03-14 15:25:44.208 KKBOX[5786:187150] test
2016-03-14 15:25:44.208 KKBOX[5786:187150] 4
2016-03-14 15:25:44.208 KKBOX[5786:187150] testtest
2016-03-14 15:25:44.209 KKBOX[5786:187150] runtime hello
Program ended with exit code: 0
#import <objc/runtime.h>
@interface MyClass : NSObject
@end
@implementation MyClass
@end
void myMethodIMP(id self,SEL _cmd){
NSLog(@"runtime hello");
}
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
NSLog(@"Hello, World!");
NSLog(@"%s",@selector(test));
NSLog(@"%lu",strlen(@selector(test)));
char *s = calloc(100, sizeof(char));
strcat(s,@selector(test));
strcat(s,@selector(test));
NSLog(@"%s",s);
free(s);
class_addMethod([MyClass class], @selector(myMethod), (IMP)myMethodIMP, "v@:");
MyClass *myObject = [[MyClass alloc] init];
[myObject performSelector:@selector(myMethod)];
}
return 0;
}
运行结果
2016-03-14 15:25:44.207 KKBOX[5786:187150] Hello, World!
2016-03-14 15:25:44.208 KKBOX[5786:187150] test
2016-03-14 15:25:44.208 KKBOX[5786:187150] 4
2016-03-14 15:25:44.208 KKBOX[5786:187150] testtest
2016-03-14 15:25:44.209 KKBOX[5786:187150] runtime hello
Program ended with exit code: 0
相关文章推荐
- eclipse fat jar 打包插件
- Java 中的语法糖 (Syntactic Sugar)
- Java heap space解决方法
- Python学习第九天
- springMVC工作原理
- qt 鼠标响应
- Java day03 进制转换(3)索引表 数组实现 十进制向各进制转换
- E96系列电阻代码表
- Java day03 进制转换(2)索引表 数组实现
- php和C# 面向对象继承
- JAVA day03 数组的应用,查找,求最值与基本排序方式
- python中控制逻辑语句
- Java day03 数组的定义和初始化
- Eclipse Che安装入门和使用(一)
- 20145303刘俊谦 《Java程序设计》第2周学习总结
- windows下python 安装igraph库报错 Cannot find the C core of igraph on this system using pkg-config.的解决
- 那些年一起踩过的坑 — java 自动装箱拆箱问题
- mysql和php
- yii2-user 扩展插件的一些坑
- python(5)-正则表达式