Invocation   调用的意思。

可想而知NSInvocation 是一个 方法调用 封装的类。

这体现了  面向对象的思想, 及一切皆对象。函数也不例外。

一般编程中,应该很少用到这个。 但是要编写 抽象度高的 框架,或 代码。 这个是必不可少的。

跟 c# , java 里的 反射 类似 。 动态访问 和 调用方法。

下面介绍下简单使用。。

用之前可以先 自己想下。如果让你 来 封装一个函数 作为一个类 ,都需要什么。

funObj  函数 对象

funObj 属性

funObj 行为(方法fun)

首先属性,需要 函数的一个描述:

{

1,唯一标示 一个函数

2,函数 在 程序里的 调用指针。

}

函数的参数:

{

arg 1

arg 2

}

函数的返回值:

{

returnValue

}

函数的调用对象

{

要调用方法的外部对象。

}

其次   函数对象的行为(fun)

{

有了足够多的信息后,我们就可以 拿到 外部调用对象 去 内存里找 函数的调用地址, 加上函数参数、返回值。 来调用函数。

}

以上这些,在面向对象的语言里 可能已经为我们封装了,OC 中的NSInvocation 就是。

对于调用一个 有两个参数以上的   函数,我们可以这样:

-( id )fun :(id) a :(id)b ......{

}

[self    fun:a : b .....]; //很简单啊。这是在确定的情况下。不确定呢,

使用

[self performSelector:@selector() withObject: .....];

很遗憾 只能传递一个参数, 除非你把 a,b 参数放倒一个  集合中。 

使用NSInvocation 
-(NSString *)customMethod:(NSString *)arg1 otherArg:(NSString *)arg2{

    return [NSString stringWithFormat:@"%@%@",arg1,arg2];

}

    SEL customSel = @selector(customMethod: otherArg:);

    NSMethodSignature * customSig = [self methodSignatureForSelector:customSel];

    NSInvocation *customInvocation =  [NSInvocation  invocationWithMethodSignature:customSig ];
NSString *arg1 = @"NS";
NSString *arg2 = @"Invocation"; [customInvocation setTarget:self];
[customInvocation setReturnValue:@encode(NSString)]; [customInvocation setSelector:customSel];
[customInvocation setArgument:&arg1 atIndex:];
[customInvocation setArgument:&arg2 atIndex:]; [customInvocation invoke];

SEL customSel = @selector(customMethod: otherArg:);

 

关于 NSInvocation的更多相关文章

  1. Objective-C中NSInvocation的使用

    OC中调用方法某个对象的消息呦两种方式: #1. performanceSelector: withObject: #2. NSInvocation. 第一个PerformaceSelector比较常 ...

  2. iOS开发——网络篇——UIWebview基本使用,NSInvocation(封装类),NSMethodSignature(签名),JavaScript,抛异常,消除警告

    一.UIWebView简介 1.UIWebView什么是UIWebViewUIWebView是iOS内置的浏览器控件系统自带的Safari浏览器就是通过UIWebView实现的 UIWebView不但 ...

  3. NSInvocation

    NSInvocation 基本简介 NSInvocation是一个静态描绘的OC消息,也就是说,它是一个动作,这个动作可以变成一个对象.NSInvocation对象在对象和对象之间和应用程序和应用程序 ...

  4. IOS NSInvocation用法简介

    IOS NSInvocation用法简介 2012-10-25 19:59 来源:博客园 作者:csj007523 字号:T|T [摘要]在 iOS中可以直接调用某个对象的消息方式有两种,其中一种就是 ...

  5. NSInvocation Basics

    In this article I'm going to cover the basics and usages of NSInvocation. What is NSInvocation? Appl ...

  6. NSInvocation的使用(转)

    转载自:http://www.cnblogs.com/pengyingh/articles/2359199.html http://blog.iosxcode4.com/?p=125 在 iOS中可以 ...

  7. ios NSMethodSignature and NSInvocation 消息转发

    1.首先获取消息转发时连个函数内部具体内容 MARK:这里是拿[@"xxxxx" length]调用拿来举例说明 (lldb) po signature <NSMethodS ...

  8. 利用NSInvocation对方法进行抽象,实现对方法的加锁

    我们在实际开发中须要对离散的方式加锁实现线程安全,当然我们有多种实现方式,这仅仅是当中一种,使用起来比較方便 + (id)performSelectorWithTarget:(id)target se ...

  9. iOS NSInvocation的学习

    用途: NSInvocation的作用和performSelector:withObject:的作用是一样的:用于iOS编程中调用某个对象的消息. performSelector:withObject ...

  10. 第16月第8天 NSInvocation存储 函数指针 va_arg lldb

    1.NSInvocation存储 -(void)setInvok:(id)target sel:(SEL)sel key:(id)key { if(!target) return; NSMethodS ...

随机推荐

  1. 前端安全系列(一):如何防止XSS攻击?

    原文:https://my.oschina.net/meituantech/blog/2218539 前端安全 随着互联网的高速发展,信息安全问题已经成为企业最为关注的焦点之一,而前端又是引发企业安全 ...

  2. Configuring the launch of the remote virtual machine to debug

    Options need to be added to the standard launch of a virtual machine (VM) to enable the debugging ar ...

  3. 〖Linux〗使用命令行切换触摸板的状态on/off/toggle

    最近发现在Ubuntu13.10中使用Fn+F9对触摸板的控制操作不灵了: 并且在黑夜.外置键盘时,按下这个组合键也很不方便,由此便想到使用命令行来切换触摸板状态: 脚本:~/bin/touchpad ...

  4. 【Linux】使用cat命令创建文本文件

    在Linux界面输入 Linux:/usr/test # cat >test01.sh 接着按回车,输入内容:"echo hello world !" 回车后按 ctrl+d ...

  5. python之模块colorsys颜色转换模块 暂不了解

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #python之模块colorsys #颜色转换模块 #colorsys import colorsys 针对 ...

  6. excel怎么在插入的方框上打勾

    本例主要介绍如何在excel中插入带对勾的方框.   工具/原料 Excel 操作步骤:  在编辑Excel表格模板时,比如说简历.人力信息登记表等,经常需要有一些可选项,如下例的婚姻状况就包括“有配 ...

  7. [抄]OKR

    OKR是Objective Key Result KPI是KeyPointIndicator OKR概览 OKR是一个目标管理工具.即目标与关键成果法,是一套明确和跟踪目标及其完成情况的管理工具和方法 ...

  8. LATeX 插入脚注

    LATeX 插入脚注: 使用  \footnote{...注释内容} 命令: To maximize the lower-bound $ we employ conjugate gradient me ...

  9. 【RS】Modeling User Exposure in Recommendation - 在推荐中建模用户的暴露程度

    [论文标题]Modeling User Exposure in Recommendation (2016-WWW) [论文作者]Dawen Liang,Laurent Charlin,James Mc ...

  10. jQuery UI API - 可拖拽小部件(Draggable Widget)(转)

    所属类别 交互(Interactions) 用法 描述:允许使用鼠标移动元素. 版本新增:1.0 依赖: UI 核心(UI Core) 部件库(Widget Factory) 鼠标交互(Mouse I ...