运行时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全球开发者在线会议上宣 ...
随机推荐
- 将/home目录从单独的分区迁移回/目录下
安装系统的时候, 将/, swap, /home这三个目录放在了三个不同的分区, 现在希望将/home目录移回/目录下. 1. umount /home, 然后在/目录下创建/home_new, 通过 ...
- Apache Spark技术实战之5 -- SparkR的安装及使用
欢迎转载,转载请注明出处,徽沪一郎. 概要 根据论坛上的信息,在Sparkrelease计划中,在Spark 1.3中有将SparkR纳入到发行版的可能.本文就提前展示一下如何安装及使用SparkR. ...
- iOS开发UI篇—Quartz2D使用(绘制基本图形)
iOS开发UI篇—Quartz2D使用(绘制基本图形) 一.简单说明 图形上下文(Graphics Context):是一个CGContextRef类型的数据 图形上下文的作用:保存绘图信息.绘图状态 ...
- maketrans translate
1. makestrans()用法 语法: str.maketrans(intab, outtab]); Python maketrans() 方法用于创建字符映射的转换表,对于接受两个参数的最简单的 ...
- 《Linux内核分析》第七周 读书笔记
<深入理解计算机系统>CHAPTER7阅读梳理 [学习时间:3hours] [学习内容:链接需要的代码&数据:链接机制:链接生成的目标文件] 一.链接概述 1.链接 定义:链接是将 ...
- kibana使用操作部分
1.kibana的概念及特点. 概念:数据可视化平台工具 特点: - 灵活的分析和可视化平台 - 实时总结和流数据的图表 - 为不同的用户显示直观的界面 - 即时分享和嵌入的仪表板 2.kib ...
- centos 下建用户 shell编程
useradd 用户名 passwd 用户名 cat /etc/passwd 查看用户信息 删除用户 userdel -r 加一个 -r 表示把用户及用户的主目录都删除 su 切换用户 sud ...
- http-2.4.18 安装
编译最新的httpd 2.4.18 编译提示apr版本低于1.4 rpm repo源是1.39的 因此编译apr 下载 apr apr-util apr-iconv [root@ansibl ...
- RML-怎样的语句会被归纳为同一类型(Unique Batches)
我们知道使用RML工具分析跟踪数据(.TRC),其中的"Unique Batches",就是一个关于Batch级别的报表,Batch级别的报表针对的是存储过程或是一个TSQL Ba ...
- Cross-site Scripting (XSS) 阅读笔记
本文源自 https://www.owasp.org/index.php/Cross-site_Scripting_%28XSS%29 通过阅读和翻译,并按照自己的理解,整理成如下文档. 概述 XSS ...