运行时c函数
// 修改isa,本质就是改变当前对象的类名
object_setClass(self, [XMGKVONotifying_Person class]);
// self动态添加关联
// id object:给哪个对象添加关联属性
// key:属性名
// value:关联值
//objc_setAssociatedObject(id object, const void *key, id value, objc_AssociationPolicy policy)
objc_setAssociatedObject(self, @"observer", observer, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
// self获取关联
//objc_getAssociatedObject(id object, const void *key)
id observer = objc_getAssociatedObject(self, @"observer");
默认情况下,如果是以[object message]的方式调用方法,如果object无法响应message消息时,编译器会报错。但如果是以perform…的形式来调用,则需要等到运行时才能确定object是否能接收message消息。如果不能,则程序崩溃。
通常,当我们不能确定一个对象是否能接收某个消息时,会先调用respondsToSelector:来判断一下。如下代码所示:
if ([self respondsToSelector:@selector(method)]) {
[self performSelector:@selector(method)];
}
摘录其它博客:
@implementation HYBMethodLearn
- (int)testInstanceMethod:(NSString *)name andValue:(NSNumber *)value {
NSLog(@"%@", name);
return value.intValue;
}
- (void)getMethods {
unsigned int outCount = ;
Method *methodList = class_copyMethodList(self.class, &outCount);
for (unsigned int i = ; i < outCount; ++i) {
Method method = methodList[i];
SEL methodName = method_getName(method);
NSLog(@"方法名:%@", NSStringFromSelector(methodName));
// 获取方法的参数类型
unsigned int argumentsCount = method_getNumberOfArguments(method);
char argName[] = {};
for (unsigned int j = ; j < argumentsCount; ++j) {
method_getArgumentType(method, j, argName, );
NSLog(@"第%u个参数类型为:%s", j, argName);
memset(argName, '\0', strlen(argName));
}
char returnType[] = {};
method_getReturnType(method, returnType, );
NSLog(@"返回值类型:%s", returnType);
// type encoding
NSLog(@"TypeEncoding: %s", method_getTypeEncoding(method));
}
free(methodList);
}
+ (void)test {
HYBMethodLearn *m = [[HYBMethodLearn alloc] init];
// [m getMethods];
((void (*)(id, SEL))objc_msgSend)((id)m, @selector(getMethods));
// 这就是为什么有四个参数的原因
int returnValue = ((int (*)(id, SEL, NSString *, NSNumber *))
objc_msgSend)((id)m,
@selector(testInstanceMethod:andValue:),
@"标哥的技术博客",
@);
NSLog(@"return value is %d", returnValue);
// 获取方法
Method method = class_getInstanceMethod([self class], @selector(testInstanceMethod:andValue:));
// 调用函数
returnValue = ((int (*)(id, Method, NSString *, NSNumber *))method_invoke)((id)m, method, @"测试使用method_invoke", @);
NSLog(@"call return vlaue is %d", returnValue);
}
给NSMutableArray添加分类
#import "NSMutableArray+Swizzling.h"
#import <objc/runtime.h>
#import <objc/message.h> @implementation NSMutableArray (Swizzling)
+(void)load
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{ //NSMutableArray ---->__NSArrayM
Method method=class_getInstanceMethod(objc_getClass("__NSArrayM"), @selector(objectAtIndex:)); Method method2=class_getInstanceMethod(objc_getClass("__NSArrayM"), @selector(fdc_objectAtIndex:)); method_exchangeImplementations(method, method2);
});
}
-(id)fdc_objectAtIndex:(NSInteger)index
{
// NSLog(@"%s",__func__); 千万别加上这句 不然有bug
if (self.count == ) {
NSLog(@"%s can't get any object from an empty array", __FUNCTION__);
return nil;
} if (index > self.count) {
NSLog(@"%s index out of bounds in array", __FUNCTION__);
return nil;
}
return [self fdc_objectAtIndex:index];
}
@end
Student *obj=[[Student alloc]init];
NSLog(@"instance :%p", obj);
NSLog(@"---------------------------------------------");
NSLog(@"class :%p", object_getClass(obj));
NSLog(@"meta class :%p", object_getClass(object_getClass(obj)));
NSLog(@"root meta :%p", object_getClass(object_getClass(object_getClass(obj))));
NSLog(@"root meta's meta :%p", object_getClass(object_getClass(object_getClass(object_getClass(obj)))));
NSLog(@"---------------------------------------------");
NSLog(@"class :%p", [obj class]);
NSLog(@"meta class :%p", [[obj class] class]);
NSLog(@"root meta :%p", [[[obj class] class] class]);
NSLog(@"root meta's meta :%p", [[[[obj class] class] class] class]);
-- ::05.074 测试[:] instance :0x7fed2276d9c0
-- ::05.075 测试[:] ---------------------------------------------
-- ::05.075 测试[:] class :0x101417060
-- ::05.075 测试[:] meta class :0x101417038
-- ::05.076 测试[:] root meta :0x101c75198
-- ::05.076 测试[:] root meta's meta :0x101c75198
-- ::05.076 测试[:] ---------------------------------------------
-- ::05.076 测试[:] class :0x101417060
-- ::05.076 测试[:] meta class :0x101417060
-- ::05.077 测试[:] root meta :0x101417060
-- ::05.077 测试[:] root meta's meta :0x101417060
类簇:
类簇的概念:一个父类有好多子类,父类在返回自身对象的时候,向外界隐藏各种细节,根据不同的需要返回的其实是不同的子类对象,这其实就是抽象类工厂的实现思路,iOS最典型的就是NSNumber。
NSNumber,NSArray,NSDictionary、NSString、NSTimer…这说明大多数的OC类都是类簇实现的;
NSNumber *intNum = [NSNumber numberWithInt:];
NSNumber *boolNum = [NSNumber numberWithBool:YES];
NSLog(@"intNum :%@", [intNum class]);
NSLog(@"boolNum:%@", [boolNum class]);
NSLog(@"Superclass:%@", class_getSuperclass([boolNum class]));
NSLog(@"Superclass:%@", class_getSuperclass([intNum class]));
-- ::21.166 测试[:] intNum :__NSCFNumber
-- ::21.167 测试[:] boolNum:__NSCFBoolean
-- ::21.167 测试[:] Superclass:NSNumber
-- ::21.167 测试[:] Superclass:NSNumber
运行时c函数的更多相关文章
- 利用Objective-C运行时hook函数的三种方法
版权声明:转载请注明出处:http://blog.csdn.net/hursing 方法一,hook已有公开头文件的类: 首先写一个Utility函数: #import <objc/runtim ...
- runtime运行时
/** * Describes the instance variables declared by a class. * * @param cls The class to inspect. * @ ...
- 【原】iOS动态性(五)一种可复用且解耦的用户统计实现(运行时Runtime)
声明:本文是本人 编程小翁 原创,转载请注明. 为了达到更好的阅读效果,强烈建议跳转到这里查看文章. iOS动态性是我的关于iOS运行时的系列文章,由浅入深,从理论到实践.本文是第5篇.有兴趣可以看看 ...
- iOS 运行时runtime控制私有变量以及私有方法
OC是运行时语言,只有在程序运行时,才会去确定对象的类型,并调用类与对象相应的方法.利用runtime机制让我们可以在程序运行时动态修改类.对象中的所有属性.方法,就算是私有方法以及私有属性都是可以动 ...
- Objective C运行时(runtime)
#import <objc/runtime.h> void setBeingRemoved(id __self, SEL _cmd) { NSLog(@"------------ ...
- 【原】iOS动态性(二):运行时runtime初探(强制获取并修改私有变量,强制增加及修改私有方法等)
OC是运行时语言,只有在程序运行时,才会去确定对象的类型,并调用类与对象相应的方法.利用runtime机制让我们可以在程序运行时动态修改类.对象中的所有属性.方法,就算是私有方法以及私有属性都是可以动 ...
- iOS运行时Runtime浅析
运行时是iOS中一个很重要的概念,iOS运行过程中都会被转化为runtime的C代码执行.例如[target doSomething];会被转化成objc)msgSend(target,@select ...
- Objective C运行时(runtime)技术的几个要点总结
前言: Objective C的runtime技术功能非常强大,能够在运行时获取并修改类的各种信息,包括获取方法列表.属性列表.变量列表,修改方法.属性,增加方法,属性等等,本文对相 ...
- 《转》.NET开源核心运行时,且行且珍惜
转载自infoQ 背景 InfoQ中文站此前报道过,2014年11月12日,ASP.NET之父.微软云计算与企业级产品工程部执行副总裁Scott Guthrie,在Connect全球开发者在线会议上宣 ...
随机推荐
- mysql5.5手册读书日记(1)
<?php //mysql语句使用技巧 /* * 我的数据库是5.5.2 * * 查询当前用户的登陆的名字 * select user(); * * 查询当前mysql服务器时间和服务器版本 * ...
- EL表达式与JSTL
内容包括 EL表达式 EL函数库 JSTL 核心标签库 格式化标签库 SQL标签库 XML标签库 自定义标签库 EL表达式 EL是Expression Language的是缩写,是JSP页面编写的一种 ...
- Apache Spark源码走读之23 -- Spark MLLib中拟牛顿法L-BFGS的源码实现
欢迎转载,转载请注明出处,徽沪一郎. 概要 本文就拟牛顿法L-BFGS的由来做一个简要的回顾,然后就其在spark mllib中的实现进行源码走读. 拟牛顿法 数学原理 代码实现 L-BFGS算法中使 ...
- centos yum 使用笔记
yum 参数说明yum -y # 表示自动选择 基本使用# yum -y install 包名(支持*) :自动选择y,全自动# yum install 包名(支持*) :手动选择y or n# yu ...
- CentOS下搭建nginx+php环境
一.下载安装nginx 参见 http://www.cnblogs.com/kreo/p/4378086.html 不再赘述 二.下载php #下载 wget http://bg2.php.net/d ...
- 微信蓝牙BLE接入调试指引 硬件篇
1 平台框架简介 微信蓝牙BLE由三个模块组成,分别是蓝牙设备.微信和第三方服务器,如下图: 蓝牙设备与微信之间的通信是通过蓝牙GATT协议进行. 微信与第三方服器之间的通信是通过网络http 接口进 ...
- 解决git .ignore文件无效
在用 Git 进行代码管理的时候,我们会用 .gitignore 文件来描述哪些文件是不需要进行版本管理的,也就是被忽略掉. 如果我们在第一次提交的时候,忘记添加 .gitignore 文件或者在首次 ...
- 周记:Linux下C编程
也啥都没干,计划一个都没干.吼吼-- 周六去看[21天学通Linux_C编程]笔记: 突然都对这本书没信心了.刚开始,就出现的不满如下:1.创建vim启动器时候,类型要选择[终端下的应用程序]没说,虽 ...
- oracle 判断字符串是否日期格式
select case when to_char(TO_DATE(NVL('2015- 8', 'a'), 'yyyy-mm'),'yyyy-mm')='2015- 8' then 1 else 0 ...
- golang protobuf SetExtension
对golang protobuf 的扩展字段赋值时候一直提示proto: bad extension value type clkUrl:="z.cn" proto.SetExte ...