// 修改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函数的更多相关文章

  1. 利用Objective-C运行时hook函数的三种方法

    版权声明:转载请注明出处:http://blog.csdn.net/hursing 方法一,hook已有公开头文件的类: 首先写一个Utility函数: #import <objc/runtim ...

  2. runtime运行时

    /** * Describes the instance variables declared by a class. * * @param cls The class to inspect. * @ ...

  3. 【原】iOS动态性(五)一种可复用且解耦的用户统计实现(运行时Runtime)

    声明:本文是本人 编程小翁 原创,转载请注明. 为了达到更好的阅读效果,强烈建议跳转到这里查看文章. iOS动态性是我的关于iOS运行时的系列文章,由浅入深,从理论到实践.本文是第5篇.有兴趣可以看看 ...

  4. iOS 运行时runtime控制私有变量以及私有方法

    OC是运行时语言,只有在程序运行时,才会去确定对象的类型,并调用类与对象相应的方法.利用runtime机制让我们可以在程序运行时动态修改类.对象中的所有属性.方法,就算是私有方法以及私有属性都是可以动 ...

  5. Objective C运行时(runtime)

    #import <objc/runtime.h> void setBeingRemoved(id __self, SEL _cmd) { NSLog(@"------------ ...

  6. 【原】iOS动态性(二):运行时runtime初探(强制获取并修改私有变量,强制增加及修改私有方法等)

    OC是运行时语言,只有在程序运行时,才会去确定对象的类型,并调用类与对象相应的方法.利用runtime机制让我们可以在程序运行时动态修改类.对象中的所有属性.方法,就算是私有方法以及私有属性都是可以动 ...

  7. iOS运行时Runtime浅析

    运行时是iOS中一个很重要的概念,iOS运行过程中都会被转化为runtime的C代码执行.例如[target doSomething];会被转化成objc)msgSend(target,@select ...

  8. Objective C运行时(runtime)技术的几个要点总结

    前言:          Objective C的runtime技术功能非常强大,能够在运行时获取并修改类的各种信息,包括获取方法列表.属性列表.变量列表,修改方法.属性,增加方法,属性等等,本文对相 ...

  9. 《转》.NET开源核心运行时,且行且珍惜

    转载自infoQ 背景 InfoQ中文站此前报道过,2014年11月12日,ASP.NET之父.微软云计算与企业级产品工程部执行副总裁Scott Guthrie,在Connect全球开发者在线会议上宣 ...

随机推荐

  1. JS控制打印指定div

    <html><head><script language="javascript">function printdiv(printpage){v ...

  2. 通过方法名(字符串)执行Objective-C方法

    SEL selector = NSSelectorFromString(@"方法名"); if ([self respondsToSelector:selector]){ //判断 ...

  3. switch多分支语句

    1.switch多分支语句的语法 switch(表达式){ case 常量值:要执行的语句; break; case 常量值:要执行的语句; break; case 常量值:要执行的语句; break ...

  4. apk反编译、smali修改、回编译笔记

    最近下了一个apk程序,但是一启动会弹出一个流氓广告.这个广告不是原厂商加的,而是有人在原有apk程序的基础上,加了一个壳,让apk先启动他加的广告,再启动原来的程序,很恶心.于是想去掉它. 试了几个 ...

  5. Java学习-048-插件应用之 Find Bugs

    FindBugs 是一个静态分析工具,它可以检查类或者 JAR 文件,将字节码与一组缺陷模式进行对比以发现可能的问题,使用 FindBugs 可以在不实际运行程序的情况对软件进行分析.使用时最好将字节 ...

  6. Qt字符串类——2. 查询字符串数据

    (1)函数QString::startsWith()判断一个字符串是否以某个字符串开头.此函数具有两个参数,第一个参数指定了一个字符串,第二个参数指定是否大小写敏感(默认情况下,是大小写敏感的),例如 ...

  7. DuiLib学习笔记2——写一个简单的程序

    我们要独立出来自己创建一个项目,在我们自己的项目上加皮肤这才是初衷.我的新建项目名为:duilibTest 在duilib根目录下面有个 Duilib入门文档.doc 我们就按这个教程开始入门 首先新 ...

  8. JavaScript读二进制文件并用ajax传输二进制流

    综合网上多个教程,加上自己实践得出的方法,目前能够兼容谷歌.IE11.IE10. htmlbody里的内容,没什么特殊的. <div id="dConfirm"> &l ...

  9. c++11 auto unique_ptr 等

    c++11 条款21:尽量使用std::make_unique和std::make_shared而不直接使用new c++11 条款18: 使用std::unique_ptr来进行独享所有权的资源管理 ...

  10. winform 计算器

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...