zoukankan      html  css  js  c++  java
  • IOS NSInvocation用法



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

    一种是performSelector:withObject:

    再一种就是NSInvocation

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

    main.h

    1. #import <Foundation/Foundation.h>  
    2. #import "MyClass.h"  
    3.   
    4. int main (int argc, const char * argv[])  
    5. {  
    6.   
    7.     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];  
    8.   
    9.     MyClass *myClass = [[MyClass alloc] init];  
    10.     NSString *myString = @"My string";  
    11.       
    12.     //普通调用  
    13.     NSString *normalInvokeString = [myClass appendMyString:myString];  
    14.     NSLog(@"The normal invoke string is: %@", normalInvokeString);  
    15.       
    16.     //NSInvocation调用  
    17.     SEL mySelector = @selector(appendMyString:);  
    18.     NSMethodSignature * sig = [[myClass class]   
    19.                                instanceMethodSignatureForSelector: mySelector];  
    20.       
    21.     NSInvocation * myInvocation = [NSInvocation invocationWithMethodSignature: sig];  
    22.     [myInvocation setTarget: myClass];  
    23.     [myInvocation setSelector: mySelector];  
    24.       
    25.     [myInvocation setArgument: &myString atIndex: 2];  
    26.       
    27.     NSString * result = nil;      
    28.     [myInvocation retainArguments];      
    29.     [myInvocation invoke];  
    30.     [myInvocation getReturnValue: &result];  
    31.     NSLog(@"The NSInvocation invoke string is: %@", result);  
    32.   
    33.     [myClass release];  
    34.       
    35.     [pool drain];  
    36.     return 0;  
    37. }  

    MyClass.h
    1. #import <Foundation/Foundation.h>  
    2.   
    3.   
    4. @interface MyClass : NSObject {  
    5.       
    6. }  
    7.   
    8. - (NSString *)appendMyString:(NSString *)string;  
    9.   
    10. @end  

    MyClass.m
    1. #import "MyClass.h"  
    2.   
    3.   
    4. @implementation MyClass  
    5.   
    6. - (id)init  
    7. {  
    8.     self = [super init];  
    9.     if (self) {  
    10.         // Initialization code here.  
    11.     }  
    12.       
    13.     return self;  
    14. }  
    15.   
    16. - (NSString *)appendMyString:(NSString *)string  
    17. {  
    18.     NSString *mString = [NSString stringWithFormat:@"%@ after append method", string];  
    19.       
    20.     return mString;  
    21. }  
    22.   
    23. - (void)dealloc  
    24. {  
    25.     [super dealloc];  
    26. }  
    27.   
    28. @end  

    这里说明一下[myInvocation setArgument: &myString atIndex: 2];为什么index从2开始 ,原因为:0 1 两个参数已经被target 和selector占用

  • 相关阅读:
    百度地图 导航
    设置cookies第二天0点过期
    GDI+一般性错误(A generic error occurred in GDI+)
    图片上传裁剪(asp.net)
    LeetCode_7. Reverse Integer
    LeetCode_1. Two Sum
    JAVA 基础编程练习题50 【程序 50 文件 IO】
    JAVA 基础编程练习题49 【程序 49 子串出现的个数】
    JAVA 基础编程练习题48 【程序 48 加密】
    JAVA 基础编程练习题47 【程序 47 打印星号】
  • 原文地址:https://www.cnblogs.com/csj007523/p/2608432.html
Copyright © 2011-2022 走看看