runtime消息发送
来源:互联网 发布:淘宝菲艺轩怎么样 编辑:程序博客网 时间:2024/04/30 19:26
一、runtime简介
- RunTime简称运行时。OC就是
运行时机制
,也就是在运行时候的一些机制,其中最主要的是消息机制。对于C语言,函数的调用在编译的时候会决定调用哪个函数
。对于OC的函数,属于动态调用过程
,在编译的时候并不能决定真正调用哪个函数,只有在真正运行的时候才会根据函数的名称找到对应的函数来调用。
1.发送消息
OC方法的调用就是让对象发消息,objc_msgSend()方法就是 用来发送消息的,Permissions *p = [[Permissions alloc] init];
[p waitUrgeOrder];其实最后会转成
objc_msgSend(p,@selector(waitUrgeOrder));
如果有参数后面也可填入参数
SEL sel = @selector(alertWithString:); objc_msgSend([Tools class], sel, @"参数");
最近在ios8时,发现如下报错:
Too many arguments to function call, expected 0, have 3
解决方法
((void (*)(id, SEL, id))(void *) objc_msgSend)((id)[Tools class], sel, @"参数");
objc_msgSend(receiver, selector, arg1, arg2, ...)
参数:receiver 接受对象
selector 方法选择器
arge1 参数
创建类 Tools
.h中有个弹窗方法 需要传人参数str
#import <Foundation/Foundation.h>@interface Tools : NSObject+(void)alertWithString:(NSString *)str;@end.m
#import "Tools.h"@implementation Tools+(void)alertWithString:(NSString *)str{ UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"提示" message:str delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil]; [alert show];}@end
其他类中调用
- (instancetype)init{ self = [super init]; if (self) { [Tools alertWithString:@"111"]; SEL sel = @selector(alertWithString:); ((void (*)(id, SEL, id))(void *) objc_msgSend)((id)[Tools class], sel, @"你好runtime"); } return self;}最终效果为
技术有限 如有什么不对的地方 希望大牛 能多多指导!!!!
0 0
- runtime objc_msgSend消息发送
- runtime消息发送
- 初识 Runtime机制 - 发送消息
- 从发送一个消息(调用一个方法)来说 Runtime
- 简谈runtime运行机制之消息发送与转发
- ios runtime 之消息发送机制三次拯救机会
- runtime从入门到精通(五)—— 消息发送和消息转发
- 神经病院Objective-C Runtime住院第二天——消息发送与转发
- iOS 开发 深入浅出Runtime运行时之官方指南翻译--消息发送
- 神经病院Objective-C Runtime住院第二天——消息发送与转发
- Runtime的入门与应用之五-消息发送与转发
- 消息发送
- 发送消息
- 发送消息
- Runtime的消息机制
- Runtime之消息机制
- Runtime 和消息机制
- Runtime之消息转发
- Reveal2通过lldb调试的方法
- 缓存雪崩,缓存穿透解决方案
- 监听EditText输入框内输入内容的变化
- 直接交换数据
- Linux源码中的mktime算法解析
- runtime消息发送
- jsonp 关于跨域
- css 3 基础选择器。
- linux下讲解MySQL安装与登录方法
- 深圳非招人企业的 培训机构---各大应届生留意
- CCF 压缩编码(石子合并问题)
- java----MD5加密
- 使用Jenkins配置自动化构建maven项目
- mysql执行两条update语句