美文网首页
runtime消息发送

runtime消息发送

作者: 半桶水技术 | 来源:发表于2017-03-10 17:41 被阅读0次

一、runtime简介

RunTime简称运行时。OC就是运行时机制,也就是在运行时候的一些机制,其中最主要的是消息机制。对于C语言,函数的调用在编译的时候会决定调用哪个函数。对于OC的函数,属于动态调用过程,在编译的时候并不能决定真正调用哪个函数,只有在真正运行的时候才会根据函数的名称找到对应的函数来调用。

1.发送消息

OC方法的调用就是让对象发消息,objc_msgSend()方法就是 用来发送消息的,

Permissions*p = [[Permissionsalloc]init];

[pwaitUrgeOrder];

其实最后会转成

objc_msgSend(p,@selector(waitUrgeOrder));

如果有参数后面也可填入参数

SELsel =@selector(alertWithString:);

objc_msgSend([Toolsclass], sel,@"参数");

最近在ios8时,发现如下报错:

Too many arguments to function call, expected 0, have 3

解决方法

((void(*)(id,SEL,id))(voidvoid*) objc_msgSend)((id)[Toolsclass], sel,@"参数");

objc_msgSend(receiver, selector, arg1, arg2, ...)

参数:receiver   接受对象

selector    方法选择器

arge1     参数

例如:

创建类 Tools

.h中有个弹窗方法 需要传人参数str

#import 

@interfaceTools : NSObject

+(void)alertWithString:(NSString*)str;

@end

.m

#import "Tools.h"

@implementationTools

+(void)alertWithString:(NSString*)str

{

UIAlertView* alert = [[UIAlertViewalloc]initWithTitle:@"提示"message:strdelegate:nilcancelButtonTitle:@"确定"otherButtonTitles:nilnil];

[alertshow];

}

@end

其他类中调用

- (instancetype)init

{

self= [superinit];

if(self) {

[ToolsalertWithString:@"111"];

SELsel =@selector(alertWithString:);

((void(*)(id,SEL,id))(voidvoid*) objc_msgSend)((id)[Toolsclass], sel,@"你好runtime");

}

returnself;

}

最终效果为

技术有限 如有什么不对的地方 希望大牛 能多多指导!!!!

相关文章

  • Runtime --- 消息发送

    上篇内容我们主要了解了objc_msgSend方法的几个参数和objc_class的结构本篇内容我们一起了解 消息...

  • runtime消息发送

    一、runtime简介 RunTime简称运行时。OC就是运行时机制,也就是在运行时候的一些机制,其中最主要的是消...

  • Runtime 消息发送

    1、isa 详解 isa 在 arm64 架构之前就是一个普通的指针,存储着 Class、Meta-Class 对...

  • Runtime消息发送

    对象方法的调用就是向这个对象发送消息 objc_method这个结构体的内容: SEL method_name 方...

  • Runtime — 消息发送

    一、Runtime 1. Runtime介绍 Objective-C 是一门动态语言,而承载整个 OC 动态特性的...

  • Runtime-原理

    runtime初探对象与方法的本质runtime-消息发送runtime-动态方法解析runtime-消息转发 r...

  • runtime objc_msgSend使用

    前言 想要通过runtime发送消息,就必须要掌握runtime如何发送消息,是调用哪个函数?又是如何调用的?本篇...

  • objc_msgSend发送消息 动态添加函数

    前言想要通过runtime发送消息,就必须要掌握runtime如何发送消息,是调用哪个函数?又是如何调用的?本篇文...

  • iOS objc_msgSend详解

    前言想要通过runtime发送消息,就必须要掌握runtime如何发送消息,是调用哪个函数?又是如何调用的?本篇文...

  • runtime的消息机制

    任何方法调用本质:发送一个消息,用runtime发送消息,OC底层实现通过runtime实现; 我们平时书写的代码...

网友评论

      本文标题: runtime消息发送

      本文链接:https://www.haomeiwen.com/subject/kfrogttx.html