NSInvocation的使用

ISInvocationHookProxy:挂钩目标的每个 NSInvocation 的代理对象 ISInvocationHookProxy 挂钩目标的每个 NSInvocation 的代理对象。 用法 NSString *object = [[ NSString alloc ] init ]; ISInvocationHookProxy *proxy = [[ISInvocationHookProxy alloc ] initWithTarget: object]; proxy.hookBlock = ^( NSInvocation *invocation) { // will be called when [(NSString *)proxy length]; is executed }; [( NSString *)proxy length ]; 覆盖调用的返回值 NSString *object = @" foo " ; ISInvocationHookProxy 立即下载

在 iOS中可以直接调用 某个对象的消息 方式有2种

一种是performSelector:withObject:

再一种就是NSInvocation

第一种方式比较简单,能完成简单的调用。但是对于>2个的参数或者有返回值的处理,那就需要做些额外工作才能搞定。那么在这种情况下,我们就可以使用NSInvocation来进行这些相对复杂的操作

NSInvocation可以处理参数、返回值。会java的人都知道反射操作,其实NSInvocation就相当于反射操作。

下面这个例子描述了如何使用NSInvocation,以下例子中如果要正常运行,需要把不存在的类进行正确填写。

//方法签名类,需要被调用消息所属的类AsynInvoke ,被调用的消息invokeMethod:

NSMethodSignature *sig= [[AsynInvoke class] instanceMethodSignatureForSelector:@selector(invokeMethod:)];

//根据方法签名创建一个NSInvocation

NSInvocation *invocation=[NSInvocation invocationWithMethodSignature:sig];

//设置调用者也就是AsynInvoked的实例对象,在这里我用self替代

[invocation setTarget:self];

//设置被调用的消息

[invocation setSelector:@selector(invokeMethod:)];

//如果此消息有参数需要传入,那么就需要按照如下方法进行参数设置,需要注意的是,atIndex的下标必须从2开始。原因为:0 1 两个参数已经被target 和selector占用

NSInteger num=10;

[invocation setArgument:&num atIndex:2];

//retain 所有参数,防止参数被释放dealloc

[invocation retainArguments];

//消息调用

[invocation invoke];

//如果调用的消息有返回值,那么可进行以下处理

 

//获得返回值类型

const char *returnType = sig.methodReturnType;

//声明返回值变量

id returnValue;

//如果没有返回值,也就是消息声明为void,那么returnValue=nil

if( !strcmp(returnType, @encode(void)) ){

returnValue =  nil;

}

//如果返回值为对象,那么为变量赋值

else if( !strcmp(returnType, @encode(id)) ){

[invocation getReturnValue:&returnValue];

}

else{

//如果返回值为普通类型NSInteger  BOOL

 

//返回值长度

NSUInteger length = [sig methodReturnLength];

//根据长度申请内存

void *buffer = (void *)malloc(length);

//为变量赋值

[invocation getReturnValue:buffer];

 

 

if( !strcmp(returnType, @encode(BOOL)) ) {

returnValue = [NSNumber numberWithBool:*((BOOL*)buffer)];

}

else if( !strcmp(returnType, @encode(NSInteger)) ){

returnValue = [NSNumber numberWithInteger:*((NSInteger*)buffer)];

}

returnValue = [NSValue valueWithBytes:buffer objCType:returnType];

}

 

 

 

NSInvocation调用

CurrentDate.h

复制代码
#import  < Foundation / Foundation.h >

@interface CurrentDate : NSObject {

}
-  (NSString  * ) stringForDate: (NSDate  * )date 
usingFormatter: (NSDateFormatter 
* )formatter;

@end
复制代码

CurrentDate.m

复制代码
#import  " CurrentDate.h "

@implementation CurrentDate

-  (NSString  * ) stringForDate: (NSDate  * )date 
usingFormatter: (NSDateFormatter 
* )formatter
{
return  [formatter stringFromDate: date];
}

@end
复制代码

main.m

复制代码
#import  < Foundation / Foundation.h >
#import 
" CurrentDate.h "

// 参考: http://theocacao.com/document.page/264
int  main ( int  argc,  const char *  argv[])
{

NSAutoreleasePool 
*  pool  =  [[NSAutoreleasePool alloc] init];

// 原始调用
NSDateFormatter  *  dateFormat  =  [[NSDateFormatter alloc] 
initWithDateFormat:
@" %b %d %Y "  
allowNaturalLanguage: NO];
CurrentDate 
*  currentDateClassObject  =  [[CurrentDate alloc] init];
NSString 
*  currentDate  =  [currentDateClassObject 
stringForDate: [NSDate date] 
usingFormatter: dateFormat];
NSLog(
@" currentDate: %@ " , currentDate);


// NSInvocation调用
SEL mySelector  =  @selector(stringForDate:usingFormatter:);
NSMethodSignature 
*  sig  =  [[currentDateClassObject  class ] 
instanceMethodSignatureForSelector: mySelector];

NSInvocation 
*  myInvocation  =  [NSInvocation invocationWithMethodSignature: sig];
[myInvocation setTarget: currentDateClassObject];
[myInvocation setSelector: mySelector];

NSDate 
*  myDate  =  [NSDate date];
[myInvocation setArgument: 
& myDate atIndex:  2 ];

NSDateFormatter 
*  dateFormatter  =  [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle: NSDateFormatterMediumStyle]; 
[myInvocation setArgument: 
& dateFormatter atIndex:  3 ];

NSString 
*  result  =  nil; 
[myInvocation retainArguments]; 
[myInvocation invoke];
[myInvocation getReturnValue: 
& result];
NSLog(
@" The result is: %@ " , result);


[pool drain];
return 0 ;
}
复制代码

 

http://www.j2megame.org/index.php/content/view/2635/165.html

NSMethodSignature和NSInvocation的使用

动态调用方法时会用到,例子 

-(NSString *)myMethod:(NSString *)param1 withParam2:(NSNumber *)param2 
{ 
    NSString *result = @"objc"; 
    NSLog(@"par = %@",param1); 
    NSLog(@"par 2 = %@",param2); 
    return result; 
} 


-(void)invokeMyMethodDynamically 
{ 
    SEL selector = @selector(myMethod:withParam2:); 
    NSMethodSignature *methodSignature = [[self class] instanceMethodSignatureForSelector:selector];//获得类和方法的签名 
    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSignature]; 
    //从签名获得调用对象 
    [invocation setTarget:self]; 
    //设置target 
    [invocation setSelector:selector];//设置selector 
    NSString *returnValue = nil; 
    NSString *argument1 = @"fist"; 
    NSNumber *argument2 = [NSNumber numberWithInt:102]; 
    [invocation setArgument:&argument1 atIndex:2];//设置参数,第一个参数index为2 
    [invocation setArgument:&argument2 atIndex:3]; 
    [invocation retainArguments];//retain一遍参数 
    [invocation invoke];//调用 
    [invocation getReturnValue:&returnValue];//得到返回值,此时不会再调用,只是返回值 
    NSLog(@"return value = %@",returnValue); 
} 

另外一个例子:

SEL selector = @selector(myMethod:setValue2:);

NSMethodSignature *signature = [MyObject instanceMethodSignatureF orSelector:selector];
NSInvocation *invocation = [NSInvocation invocationWithMethodSign ature:signature];
[invocation setSelector:selector];

NSString *str1 = @"someString";
NSString *str2 = @"someOtherString";

//The invocation object must retain its arguments
[str1 retain];
[str2 retain];

//Set the arguments
[invocation setTarget:targetInstance];
[invocation setArgument:&str1 atIndex:2];
[invocation setArgument:&str2 atIndex:3];

[NSTimer scheduledTimerWithTimeIn terval:0.1 invocation:invocation repeats:YES]
云模型在综合评价过程中的应用 目录 1 云模型标尺(标准云模型)的构建方法 1.1 通过正态云的“3En 规则”来表示评价论域 1.2 通过论域的边界值隶属度相等来表示评价论域 1.3 通过黄金分割法来表示评价论域 2 确定评价结果的过程 2.1 基本云发生器 2.2 X条件云发生器 2.3 两种方法的区别 综合评价问题一般分为以下六个步骤 1、明确评价目的; 2、确定被评价对象; 3、建立评价指标体系(包括收集评价指标的原始值、评价指标的若干预处理等); 4、确立与各项评价指标相对应的权重系数; 5、选.. 阅读详情

相关推荐

四足机器人跨楼层轨迹规划:SCAN-Planner-Pure-ROS2开源项目实践指南

在机器人导航领域,路径规划是让机器人从起点安全、高效移动到目标点的核心技术。其原理通常基于环境地图(如占据栅格地图或八叉树地图),通过搜索算法(如A*、RRT*)在配置空间中找到一条无碰撞的可行路径。这项技术的价值在于赋予机器人在复杂、非结构化环境中的自主移动能力,是巡检、救援、物流等应用场景的基石。对于四足机器人而言,其运动能力超越了轮式平台,能在楼梯、斜坡等三维地形中移动,这使得传统的二维规划不再适用,需要专门的三维轨迹规划算法。SCAN-Planner-Pure-ROS2正是这样一个专注于解决四足机器

weixin_30700977的博客 475

iOS开发之NSMethodSignature(方法签名)

OC中方法调用有三种: 第一种:直接调用 - (void)viewDidLoad { [super viewDidLoad]; [self printStr1:@"hello"]; } - (void)printStr1:(NSString*)str{ NSLog(@"printStr1 %@",str); } 第二种:通过performSelector - (void)viewDidLoad { [super viewDidLoad]; [sel

逝水无情的博客 1711

OnlyOffice Docker部署踩坑实录:解决Nginx反向代理导致的editor.bin下载失败

本文详细解析了在Docker中部署OnlyOffice时,因Nginx反向代理配置不当导致editor.bin下载失败的常见问题。通过分析问题根源,提供了从Nginx代理配置、Docker网络设置到OnlyOffice容器内部调整的完整解决方案,并分享了实战调试技巧与性能优化建议,帮助用户彻底解决这一部署难题。

wine的专栏 533

使用NSInvocation 调用实例的方法,且等待获得返回值

OBJCExternInterface * ibjInter = [[OBJCExternInterface alloc] init]; //定义类的实例     SEL theSelector;     NSMethodSignature *aSignature;     NSInvocation *anInvocation;     theSelector = @selector(ReadF

lipeiran1987的专栏 725

寒假实训笔记day01——认识M4单片机之STM32F405RGT6

认识M4单片机之STM32F405RGT6(含义、规格、参数)

weixin_47200070的博客 8554

yolov7模型部署流程

yolov7部署流程

算法的猫 1402

NSInvocation的基本用法

@在 iOS中可以直接调用某个对象的消息方式有2种: 1.performSelector:withObject:                                         2.NSInvocation 第一种方式比较简单,能完成简单的调用。但是对于>2个的参数或者有返回值的处理,那就需要做些额外工作才能搞定。那么在这种情况下,我们就可以使用NSInvocation来进行这

iOS界@迷糊小书童 3193

UE5接入在线直播视频源,如hls(m3u8)格式

UE5接入在线直播视频源

专注GIS开发 9524

NSInvocation的基本使用

一. NSInvocation简介 在iOS开发中,可以直接调用方法的方式有两种:performSelector:withObject: 和 NSInvocation。 performSelector:withObject:使用简单,但缺点是只能传一个参数,大于2个参数就无法使用;NSInvocation就不一样,功能更加强大,但实现有点复杂。在IOS4.0之后,这两种直接调用对象消息的方法,

诠释完美 9630

Copilot在游戏开发中的应用:Unity脚本自动生成实践

2021年GitHub Copilot的发布,为代码自动生成带来了革命性突破。作为基于GPT模型训练的AI辅助编程工具,Copilot能根据注释、函数名、代码上下文,实时生成符合语法和逻辑的代码片段,甚至完整脚本。减少重复劳动:自动生成模板代码(如生命周期、结构),让你专注核心逻辑;降低API学习成本:对Unity特有的组件(RigidbodyAnimator)、类(),Copilot能生成符合最佳实践的调用代码;统一代码风格:通过团队共享提示词模板,让生成的代码符合项目规范;快速原型迭代。

AIGC应用创新大全的博客 1621

01 Python学习总结:Python简介及入门

Python 是一门高级编程语言,以其简洁、易读和强大的功能而广受欢迎。它适用于多种应用场景,包括 Web 开发、数据分析、人工智能、自动化脚本等。以下是 Python 的简介及入门知识总结,帮助你快速了解并开始学习 Python。

数据知道的博客 1万+

NSInvocation简单使用

在 iOS中可以直接调用 某个对象的消息 方式有2中 一种是performSelector:withObject: 再一种就是NSInvocation 第一种方式比较简单,能完成简单的调用。但是对于>2个的参数或者有返回值的处理,那就需要做些额外工作才能搞定。那么在这种情况

cancer1617的专栏 3758

OpenClaw Windows 安装指南

好的,我来一步一步带你在 Windows 上安装 OpenClaw。

m0_60605453的博客 557

【记录】NSInvocation 的使用

使用 invocationWithMethodSignature: 方法初始化一个 NSInvocation 实例 1、创建签名 NSMethodSignature *signature = [[self class] instanceMethodSignatureForSelector:selector]; if (signature == nil) { signature = [self methodSignatureForSelector:selector];

u011248221的博客 2997

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

iOS之NSInvocation详解

一、NSInvocation的基本用法在 iOS中可以直接调用某个对象的消息方式有两种:一种是performSelector:withObject;再一种就是NSInvocation。第一种方式比较简单,能完成简单的调用。但是对于&gt;2个的参数或者有返回值的处理,那performSelector:withObject就显得有点有心无力了,那么在这种情况下,我们就可以使用NSInvocation...

王者归来之英雄有梦 8063

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

iOS NSInvocation 使用

NSInvocation使用还是挺简单的,只是还是有很多开发者不知道他是干嘛的,这里做个比较。 NSInvocation能做啥? 在iOS中如果调用某个对象的消息我们经常使用系统提供的performSelector方法 performSelector: withObject: performSelector: ... performSelector这玩意是好,但蛋疼的是如果是参数是多个

木木不 718

lwip-2.0.3

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

NSInvocation 详解

/ 通过NSMethodSignature对象创建NSInvocation对象,NSMethodSignature为方法签名类。// invocation 有2个隐藏参数,所以 argument 从2开始。// 调用retainArguments之前,值为NO,调用之后值为YES。// 需要做参数类型判断然后解析成对应类型,这里默认所有参数均为OC对象。// 保留参数,它会将传入的所有参数以及target都retain一遍。// target发送消息,即target执行方法。// 发送消息,即执行方法。

2401_85131673的博客 898

NSInvocation简单总结

(1)用法  NSInvocation是调用函数的另一种方式,它将调用者,函数名,参数封装到一个对象,然后通过一个invoke函数来执行被调用的函数,其思想就是命令者模式,将请求封装成对象。 例如,有这样一个类: @implementation CA -(int)AddA:(int)a andB:(int)b {          return a + b; } @end  

酷酷的冥王星的专栏 757

NSInvocation

在 iOS中可以直接调用某个对象的消息方式有两种: 一种是performSelector:withObject; 再一种就是NSInvocation。 第一种方式比较简单,能完成简单的调用。但是对于>2个的参数或者有返回值的处理,那就需要做些额外工作才能搞定。那么在这种情况下,我们就可以使用NSInvocation来进行这些相对复杂的操作。 IOS中有一个类型是SEL,它的作用

qq_32169923的博客 388

NSInvocation使用

转自:http://www.cnblogs.com/pengyingh/articles/2359199.html http://blog.iosxcode4.com/?p=125 在 iOS中可以直接调用 某个对象的消息 方式有2种 一种是performSelector:withObject: 再一种就是NSInvocation 第一种方式比较简单,能完

zhenganzhong_csdn的博客 300

PerformSelector 与 NSInvocation

目录PerformSelectorNSMethodSignatureNSInvocationPerfromSelector 与 NSInvocation 的简单使用 PerformSelector PerformSelector 简介 performSelector: 方法等价于直接向消息接受者(即方法调用者)发送一个 aSelector 消息。例如,以下消息都会执行相同的事情: id aClone = [anObject copy]; id aClone = [anObject performSele

Airths 的博客 677
上一篇: view to image
下一篇: @selector
lidechen2014
博客等级 码龄17年 6粉丝 9原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值