在iOS中可以使用NSInvocation进行动态调用方法。

/*
NSInvocation is much slower than objc_msgSend()...
Do not use it if you have performance issues.
*/

ViewController.h

#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. //1.创建 NSMethodSignature
NSMethodSignature *sig = [self methodSignatureForSelector:@selector(sendMessageWithStr1:andStr2:andStr3:)];
if (!sig) {
[self doesNotRecognizeSelector:@selector(sendMessageWithStr1:andStr2:andStr3:)];
return;
} //2.根据 NSMethodSignature 创建 NSInvocation
NSInvocation *inv = [NSInvocation invocationWithMethodSignature:sig];
if (!inv) {
[self doesNotRecognizeSelector:@selector(sendMessageWithStr1:andStr2:andStr3:)];
return;
} //3.设置代理和Selector
[inv setTarget:self];
[inv setSelector:@selector(sendMessageWithStr1:andStr2:andStr3:)]; //4.设置参数
NSString *str1 = @"";
NSString *str2 = @"";
NSString *str3 = @""; //注意:设置参数的索引时不能从0开始,因为0已经被self占用,1已经被_cmd占用,可变参数可以使用va_list和va_start,具体参考参考YYKit中的NSObject+YYAdd.m
[inv setArgument:&str1 atIndex:];
[inv setArgument:&str2 atIndex:];
[inv setArgument:&str3 atIndex:]; //5.调用方法
[inv invoke];
} - (void)sendMessageWithStr1:(NSString *)str1 andStr2:(NSString *)str2 andStr3:(NSString *)str3{
NSLog(@"str1:%@,str2:%@,str3:%@",str1,str2,str3);
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} @end

运行截图:

参考文章:

http://www.jianshu.com/p/da96980648b6

iOS中使用NSInvocation的更多相关文章

  1. iOS 中的 HotFix 方案总结详解

    相信HotFix大家应该都很熟悉了,今天主要对于最近调研的一些方案做一些总结.iOS中的HotFix方案大致可以分为四种: WaxPatch(Alibaba) Dynamic Framework(Ap ...

  2. iOS 中各种横竖屏切换总结

    iOS 中横竖屏切换的功能,在开发iOS app中总能遇到.以前看过几次,感觉简单,但是没有敲过代码实现,最近又碰到了,demo尝试了几种情况,这里就做下总结.注意 横屏两种情况是反的你知道吗? UI ...

  3. iOS中支付宝集成

    iOS中支付宝集成 如今各种的App中都使用了三方支付的功能,现在将我在使用支付宝支付集成过程的心得分享一下,希望对大家都能有所帮助 要集成一个支付宝支付过程的环境,大致需要: 1>公司:先与支 ...

  4. iOS中数据库应用基础

    iOS 数据库入门 一.数据库简介 1.什么是数据库? 数据库(Database) 是按照数据结构来组织,存储和管理数据的仓库 数据库可以分为2大种类 关系型数据库(主流) PC端 Oracle My ...

  5. 正则表达式在iOS中的运用

    1.什么是正则表达式 正则表达式,又称正规表示法,是对字符串操作的一种逻辑公式.正则表达式可以检测给定的字符串是否符合我们定义的逻辑,也可以从字符串中获取我们想要的特定部分.它可以迅速地用极简单的方式 ...

  6. iOS中使用正则

    一.什么是正则表达式 正则表达式,又称正规表示法,是对字符串操作的一种逻辑公式.正则表达式可以检测给定的字符串是否符合我们定义的逻辑,也可以从字符串中获取我们想要的特定部分.它可以迅速地用极简单的方式 ...

  7. IOS中div contenteditable=true无法输入

    在IOS中<div contenteditable="true"></div>中点击时可以弹出键盘但是无法输入.加一个样式-webkit-user-sele ...

  8. 谈谈iOS中的屏幕方向

    众所周知,iOS中提供了[UIDevice currentDevice].orientation与[UIApplication sharedApplication].statusBarOrientat ...

  9. iOS中assign、copy 、retain等关键字的含义

    iOS中assign.copy .retain等关键字的含义  转自:http://my.oschina.net/majiage/blog/267409 assign: 简单赋值,不更改索引计数cop ...

随机推荐

  1. js的new到底干了啥 -

    javascript通过new操作符构建一个对象的步骤 <Javascript高级程序设计>的解释: 创建一个对象 将构造函数的作用域赋给新对象(把新对象作为构造函数的调用上下文,也就是t ...

  2. Ogbect对象转换为泛型对象

    相信很多人都自己写个这个转换的方法,再次附上我自己的写转换方法仅供参考. T t = BeanUtil.dbObject2Bean(obj, tClass); public static <T& ...

  3. information entropy as a measure of the uncertainty in a message while essentially inventing the field of information theory

    https://en.wikipedia.org/wiki/Claude_Shannon In 1948, the promised memorandum appeared as "A Ma ...

  4. [转】IIS:Do not nest virtual directories

    原文:https://msdn.microsoft.com/en-us/library/ms178685.aspx#Anchor_6 Configuration settings for virtua ...

  5. IO密集型操作时,为什么线程比进程更好?

    在IO密集型的操作时,进程线程都不会太占用CPU,但是进程消耗的资源比较多.

  6. Linux安装过程记录信息

    全新的linux安装完成后,会在root目录下有一下三个文件,记录了Linux的安装细节 anaconda-ks.cfg 以Kickstart配置文件的格式记录安装过程中设置的选项信息 install ...

  7. linux 中 开放端口,以及防火墙的相关命令

    最近公司需要在 生产环境上线系统,碰到一些防火墙以及开放端口的问题,在此来 复习mark下   1.设定   [root@localhost ~]# /sbin/iptables -I INPUT - ...

  8. android ui篇 自己写界面

    对于一些较为简单的界面则自己进行写. 在这里就需要了解xml文件中一些基本的属性以及android手机的知识. 一.目前手机屏幕像素密度基本有5种情况.(以下像素密度简称密度) 密度 ldpi mdp ...

  9. Android Dev Tips

    Ref:Android Service与Activity之间通信的几种方式 Ref:基础总结篇之五:BroadcastReceiver应用详解 Ref:Android Activity和Intent机 ...

  10. C# Lambda表达式与Linq

    , , , , , , }; //linq写法 var res = from i in arry select i; //lambda写法 var res = arry.Select(i => ...