Runtime-消息转发详解

iOS Runtime详解(消息机制,类元对象,缓存机制,消息转发) 原创Blog,转载请注明出处 http://blog.youkuaiyun.com/hello_hwc?viewmode=list 我的stackoverflow前言:之前一篇文章里,我详细的讲解了一些基本关键词以及基本概念,这里再简要列出来,以防有些同学看不懂。 SEL 方法的名字,可以理解为字符串指针类型 id 指向一个类的实例对象 isa 每个类的示例对象都保存的指针,指向类对象 Class 指向 阅读详情

 

消息转发分为三步,详细分解如下:

第一步

+ (BOOL)resolveClassMethod:(SEL)sel OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);
+ (BOOL)resolveInstanceMethod:(SEL)sel OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);

第二步 

- (id)forwardingTargetForSelector:(SEL)aSelector OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);

第三步 

- (void)forwardInvocation:(NSInvocation *)anInvocation OBJC_SWIFT_UNAVAILABLE("");
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector OBJC_SWIFT_UNAVAILABLE("");

首先来看看第一步,都是类方法,也就是在类方法层面来解决转发,有2个方法,第一个为解决类方法的转发,第二个为解决实例方法的转发,我们会分别举例说明,首先定义个类

///.h

@interface MessageForwarding : NSObject

@end

///.m

#import "MessageForwarding.h"
#import <objc/runtime.h>
void TestMsgForwardingIMP(id obj,SEL sel){
    NSLog(@"success1");
}
@implementation MessageForwarding
+(BOOL)resolveClassMethod:(SEL)sel{
    const char *selName = sel_getName(sel);
    if (strcmp(selName, "TestMsgForwarding") == 0) {
        class_addMethod([self superclass], sel, [self methodForSelector:@selector(TestMsgForwardingIMP1)], "v@:");
        return YES;
    }
    return [super resolveClassMethod:sel];
}
+(void)TestMsgForwardingIMP1{
    NSLog(@"success");
}
@end

 在上面类的方法里面,我们可以看到class_addMethod方法,那么我们先看下官方稳定里面怎么定义的

/** 
 * Adds a new method to a class with a given name and implementation.
 * 
 * @param cls The class to which to add a method.
 * @param name A selector that specifies the name of the method being added.
 * @param imp A function which is the implementation of the new method. The function must take at least two arguments—self and _cmd.
 * @param types An array of characters that describe the types of the arguments to the method. 
 * 
 * @return YES if the method was added successfully, otherwise NO 
 *  (for example, the class already contains a method implementation with that name).
 *
 * @note class_addMethod will add an override of a superclass's implementation, 
 *  but will not replace an existing implementation in this class. 
 *  To change an existing implementation, use method_setImplementation.
 */
OBJC_EXPORT BOOL
class_addMethod(Class _Nullable cls, SEL _Nonnull name, IMP _Nonnull imp, 
                const char * _Nullable types) 
    OBJC_AVAILABLE(10.5, 2.0, 9.0, 1.0, 2.0);

现在咱们看下执行代码和结果

[MessageForwarding performSelector:@selector(TestMsgForwarding)];

打印结果如下:

OCTest[11268:1533041] success

切换成TestMsgForwardingIMP(C的方式)这个定义

#import "MessageForwarding.h"
#import <objc/runtime.h>
void TestMsgForwardingIMP(id self,SEL sel){
    NSLog(@"success1");
}
@implementation MessageForwarding
+(BOOL)resolveClassMethod:(SEL)sel{
    const char *selName = sel_getName(sel);
    if (strcmp(selName, "TestMsgForwarding") == 0) {
//        class_addMethod([self superclass], sel, [self methodForSelector:@selector(TestMsgForwardingIMP1)], "v@:");
        class_addMethod([self superclass], sel, (IMP)TestMsgForwardingIMP, "v@:");
        return YES;
    }
    return [super resolveClassMethod:sel];
}
+(void)TestMsgForwardingIMP1{
    NSLog(@"success");
}
@end

继续执行结果如下:

OCTest[11416:1558454] success1

实例方法的转发,代码如下:

+(BOOL)resolveInstanceMethod:(SEL)sel{
    const char *selName = sel_getName(sel);
    if (strcmp(selName, "testInstanceMsgForwarding") == 0) {
        class_addMethod(self, sel,[self instanceMethodForSelector:@selector(testInstanceMsgForwardingIMP)], "v@:");
        return YES;
    }
    return [super resolveInstanceMethod:sel];
}
-(void)testInstanceMsgForwardingIMP{
    NSLog(@"instance success");
}

调用代码如下

MessageForwarding *mf = [MessageForwarding new];
[mf performSelector:@selector(testInstanceMsgForwarding)];

执行结果如下:

OCTest[11656:1582545] instance success

第二步

修改函数的执行对象,代码如下:

#import "MessageForwarding.h"
@interface ViewController ()
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self performSelector:@selector(testInstanceMsgForwarding)];
}
-(id)forwardingTargetForSelector:(SEL)aSelector{
    if (strcmp(sel_getName(aSelector), "testInstanceMsgForwarding") == 0) {
        return [MessageForwarding new];
    }
    return [super forwardingTargetForSelector:aSelector];
}
@end

执行结果如下:

OCTest[11702:1587797] instance success

第三步

随意指定对象执行相应函数,代码如下:

#import "MessageForwarding.h"
@interface ViewController ()
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self performSelector:@selector(testInstanceMsgForwarding)];
}
-(NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector{
    if (strcmp(sel_getName(aSelector), "testInstanceMsgForwarding") == 0){
        return [NSMethodSignature signatureWithObjCTypes:"v@:"];
    }
    return [super methodSignatureForSelector:aSelector];
}
- (void)forwardInvocation:(NSInvocation *)anInvocation{
    MessageForwarding *mf = [MessageForwarding new];
    if ([mf respondsToSelector:anInvocation.selector]) {
        [anInvocation invokeWithTarget:mf];
    }else{
        [self doesNotRecognizeSelector:anInvocation.selector];
    }
}
@end

执行结果如下:

OCTest[11778:1594036] instance success

 

iOS面试 - runtime & 消息转发机制 runtime 1. 什么是runtime? runtime 运行时机制,是一套比较底层的纯 C 语言 API , 属于1个 C 语言库, 包含了很多底层的 C 语言 API 。(引入 <objc/runtime.h> 或者 <objc/message.h> ) 程序运行过程时,我们平时编写的 OC 代码, 其实最终都是转成了 runtime 的 C 语言代码。 在编译... 阅读详情

相关推荐

iOS 底层探索篇 ——Runtime-消息转发

iOS 底层探索篇 ——Runtime-消息转发快速转发流程慢速转发流程 上文说到instrumentObjcMessageSends,那么这个方法是怎么来的呢。 在log_and_fill_cache里面有一个判断SUPPORT_MESSAGE_LOGGING。 进去logMessageSend方法,看到这里在往/tmp/msgSends-%d写入。 那么也就是说只要objcMsgLogEnabled,那么就会写入。搜索一下objcMsgLogEnabled。发现在instrumentObjcMess

LinShunIos的博客 639

iOS Runtime之消息转发

方法开始调用。查找方法缓存列表。找不到进入3。查找方法列表。找不到进入父类继续执行2、3。如果到根类上还找不到进入4。动态解析:即动态给方法添加一个方法实现。如果没有添加,则进入5。快速消息转发:交给其他对象调用方法。如果不行,进入6。常规消息转发:先获取方法签名,方法签名获取成功后做最后补救。补救可以是交给其他对象调用相应方法,或者是给某个方法添加方法实现。如果最后补救都不可以则进入7。崩溃,找不到方法异常。

xinxin_0的博客 629

Objective-C Runtime消息转发机制深入解析

本文还有配套的精品资源,点击获取 简介:Objective-C的Runtime系统是其实现动态特性的重要组成部分,其中消息转发机制处理运行时无法在对象中找到对应方法调用的情况。本文章深入探讨了消息发送和转发的步骤,详细说明了动态方法解析和完整消息转发等关键技术,通过示例展示了如何使用这些机制来增强程序的灵活性和健壮性。 1. Objective-C Runtime...

weixin_35756892的博客 1489

Runtime系列:消息机制【04】

OC中的方法调用的本质,都是转换为objc_msgSend函数的调用。 这里所说的消息机制就是objc_msgSend的执行流程。 objc_msgSend的执行流程可以分为3大阶段: 1、消息发送 1、首先判断消息接收者是否为nil,如果为nil则直接退出。所以存在使用 nil 调用方法,编译的时候也不会报错。 2、如果消息接收者不为空,通过isa指针找到消息接收者类对象,然后去查找方法(如果类对象没有找到方法,通过superclass指针找到父类继续查询),具体流程如下图: receiver通过isa指

chenzimin_blog 355

Runtime_消息转发机制

Runtime_消息转发机制 举例人这个类实现猫的这个类的方法

利用 Docker Distribution 搭建私有的 Docker Registry

1、拓扑

weixin_44983653的博客 1068

Runtime的本质(三)----objc_msgSend

OC中的方法调用,其实都是转换为objc_msgSend函数的调用 objc_msgSend的执行流程可以分为三大阶段: 消息发送 动态方法解析 消息转发 1.消息发送 当空对象调用方法的时候,是怎么操作的? 当调用方法的时候,执行的是objc_msgSend函数objc_msgSend(<#id _Nullable self#>, <#SEL _Nonnull op, ...#>)。 第一个参数是消息接收者,第二个参数是方法名。 我们在源码中找到objc_msgSend的具体实

IOSSHAN的博客 669

Vue集成飞书前端JSSDK

文章目录前言<1>飞书前端JSSDK<2>使用步骤1.将JSSDK引入到VUE2.读入数据总结 前言 公司需要使用飞书软件,所以转战飞书开发,也是对新知识的探索与研究,飞书SDK也一直都在更新,路虽远,行必达。 <1>飞书前端JSSDK 1、官网地址:https://open.feishu.cn/document/uYjL24iN/uITO4IjLykDOy4iM5gjM 2、当前使用的飞书前端JSSDk的版本:1.5.4 <!-- 在html文档中 --&gt

A 1万+

iOS面试题(十五)Runtime --消息转发流程

4.动态运行时(RunTime) 基础数据结构 对象、类对象、元类对象是怎么理解的?以及他们之间的关系是怎样的(实例与类对象之间的关系以及类对象与元类对象之间的关系)? OC语言中的消息传递机制是怎样的? 缓存查找 (我们进行方法查找的过程当中,如何进行缓存的方法查找?会使用到系统或者说Runtime中一个方法缓存的机制,这个机制又是怎样运行的呢?) 消息转发流程是怎样的? Me...

ochenmengo的博客 1231

睿尔曼超轻量仿人机械臂--医疗工作站运行Demo(Python)

开发环境系统:win10Python版本:python 3.11.2由于机械臂相关软件版本不定期更新,如果你使用的软件接口或协议与本文有出入,请联系官方技术人员及时更新。

realman_Rop的博客 1095

程序设计二(面向对象)_实训10_复杂类的实现_ArrayList类的实现

第1关:构造函数与析构函数的实现

2401_83406306的博客 822

Runtime详解整理(Runtime 相关数据结构、Runtime消息传递机制、Runtime消息转发机制、Runtime的应用)

Runtime Runtime是什么? 运行时(Runtime)是指将数据类型的确定由编译时推迟到了运行时 Runtime是一套比较底层的纯C语言API, 属于1个C语言库, 包含了很多底层的C语言API Object-C需要Runtime来创建类和对象,进行消息发送和转发 Runtime 相关数据结构 实例objc_object(id) struct objc_object { C...

张梦磊 1115

CMOS 半加器和全加器&&数字集成电路&& Cadence Virtuoso

数字集成电路——半加器和全加器

计科小学生的博客 1万+

iOS Runtime运行机制详解(消息传递、消息转发)

一直想发关于Runtime的文章,但是由于读的资料一多,对Runtime的理解又加深了一些,所以总是改来改去,导致一篇都没发出来(其实是因为懒)。 最近又有小伙伴问我,我也只好再次补老账了。。。 ---------啥是OC Runtime---------- OC Runtime呢,其实就是一个Runtime库。 (那啥是Runtime库呢?给你个小链接:https://www.cnblo...

ws1836300的博客 1720

WRF所需气象数据的下载方法

#数据下载是个大问题,巧妇难为无米之炊,没有数据就无法模拟啦,疫情期间国外服务器响应慢,打开网页慢都很正常啦~ WRF数据所需要的外部数据分为两种:静态地理数据,grib格式的气象数据(NAM、GFS、RUC、AGRMET等)。静态地理数据可以从WRF官网获得,比较简单,这里不做介绍。所以这篇日志重点介绍的是气象数据的获取。 提前说明,WRF官网不提供气象数据,但WRF官网提供各类气象数据的下载网址链接,所以我们下载一般还是从WRF官网开始。首先进入WRF官网,然后点击WRF ARW Uusres’ .

六出的博客 1万+

把个人化的 SQVI 报表变成可维护、可共享、可传输的 ABAP Query:从定位来源到转换落地的完整路线图

本文针对SAP系统中业务用户常用但难以维护的SQVI临时报表问题,提供了一套完整的解决方案。文章首先区分了SQVI(个人临时报表)和ABAP Query(团队可维护报表)的关键差异,重点介绍了如何通过函数模块RSAQ_DECODE_REPORT_NAME反查报表来源。对于SQVI报表,详细说明了定位创建人和转换为团队可维护的ABAP Query的具体步骤,包括Query Area切换、转换操作和后续维护方法。最后总结了常见问题的解决方案,如字段调整、口径固化和生成程序名反查等,为现场实施提供了可直接操作的完

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 303

iOS Runtime详解---动态方法决议与消息转发

我们上一篇文章讲过,通过方法编号sel找到方法实现imp,最后我们看到系统调用了这个方法: IMP lookUpImpOrForward(Class cls, SEL sel, id inst, bool initialize, bool cache, bool resolver) //在该方法的最后,系统进行了两次防crash的操作 // No implementa...

smile_sunshine 377

基于OSEK_TP.dll封装通用,多ECU自由切换的诊断函数库

本文介绍了基于CANoe内置OSEK_TP.dll库实现通用诊断通讯的方法。通过封装CAN_TP.cin顶层脚本,实现了ECU连接管理、数据收发和多ECU切换功能。系统采用字典结构存储ECU参数,支持从配置文件读取诊断ID等配置信息,通过TpCreateConnection建立TP层连接。文章详细说明了诊断请求发送函数Uds_DiagSendRequest的实现逻辑,以及如何利用回调函数处理收发事件。该方法无需依赖诊断数据库,通过底层封装实现了灵活高效的诊断通讯方案,适用于车载诊断领域的测试需求。

蚂蚁小兵 1568

iOS Runtime详解(消息机制,类元对象,缓存机制,消息转发)

原文:http://blog.youkuaiyun.com/hello_hwc?viewmode=list  前言:之前一篇文章里,我详细的讲解了一些基本关键词以及基本概念,这里再简要列出来,以防有些同学看不懂。 SEL 方法的名字,可以理解为字符串指针类型id 指向一个类的实例对象isa 每个类的示例对象都保存的指针,指向类对象Class 指向类对象 _cmd 每个OC方法都具

jesse881025的专栏 299

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

iOS开发·备战2022金三银四-runtime原理与实践: 消息转发详解篇

编程,只了解原理不行,必须实战才能知道应用场景。本系列尝试阐述runtime相关理论的同时介绍一些实战场景,而本文则是本系列的**消息转发**篇。**本文中**,第一节将介绍方法消息发送相关的概念,第二节将总结一下2\. 动态特性:方法解析和消息转发(Method Resolution,Fast Rorwarding,Normal Forwarding),第三节将介绍方法交换几种的实战场景:特定奔溃预防处理(调用未实现方法),苹果系统迭代造成API不兼容的奔溃处理,第四节将总结消息转发的机制。

Jul7day的博客 406

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

Objective-C 消息发送与转发机制原理

转载链接:Objective-C 消息发送与转发机制原理 文章目录 1.八面玲珑的 objc_msgSend 1.1.源码解析 1.2.为什么使用汇编语言 2.使用 lookUpImpOrForward 快速查找 IMP 2.1.优化缓存查找&类的初始化 2.2.继续在类的继承体系中查找 2.3.回顾 objc_msgSend 伪代码 3....

weixin_30482181的博客 231

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

iOS--Runtime

本文介绍了iOS中Runtime的概念、相关的数据结构、消息动态派发机制以及在Swift中的应用。

2301_79929304的博客 801

iOS八股文之 Runtime

这个看苹果文档的描述即可:Objective-C 语言尽可能将诸多决策从编译时和链接时推迟到运行时。只要有可能,它都会以动态方式处理事务。这意味着该语言不仅需要编译器,还需要一个运行时系统来执行已编译的代码。运行时系统就像是Objective-C 语言的一种操作系统,正是它让这种语言能够正常运行。runtime 主要用 C 语言编写(如objc.h、runtime.h中的 API),核心逻辑(如objc_msgSend)用汇编编写,目的是提升性能(汇编能直接操作寄存器,减少函数调用开销)。

RollingPin的博客 1285

iOS Runtime深入剖析:TaoToken统一Key接入下的方法交换与消息转发实战

用 objc_msgSend 探针定位 viewWillAppear 未调用问题:先配好 TaoToken 统一 Key 三件套,再复制方法交换与消息转发代码,日志验证交换生效。官网 https://taotoken.net/?utm_source=taotoken_aicg_blog_end

weixin_42584507的博客 7

iOS-Runtime消息发送、转发机制

在iOS开发中我们经常会遇到这样的crash unrecognized selector sent to instance 0x******

MinggeQingchun的博客 1340

第三篇 Runtime消息转发

发送消息时会在对应的类对象中搜索方法列表,如果找不到则会沿着继承树向上一直搜索知道继承树根部(通常为NSObject),如果还是找不到并且消息转发都失败了就回执行doesNotRecognizeSelector:方法报unrecognized selector错。 消息转发到底是什么呢?接下来将会逐一介绍最后的三次机会。 1、动态方法解析 2、备用接收者 3、完整消息转发 动态方法解析 ...

番薯大佬的专栏 1310
上一篇: Runtime-分类,修改私有属性,KVC
下一篇: React Native环境问题与解决方案
coding_most
博客等级 码龄15年 13粉丝 38原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值