iOS 类的实例方法调用 NSInvocation performSelector

在 iOS中可以直接调用 某个对象的实例方法 方式有2

一种是performSelector:withObject:

再一种就是NSInvocation

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

下面是一些代码.

CurrentDate.h

 

 

//  CurrentDate.h

//  NSInvocationTest

//  Created by binfo on 12-3-23.

//  Copyright 2012 __MyCompanyName__. All rights reserved.

#import <Foundation/Foundation.h>

@interface CurrentDate : NSObject {

    

}

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

- (NSString *)getCurData;

@end


 CurrentDate.m

 

#import "CurrentDate.h"

@implementation CurrentDate

- (NSString *)stringForDate: (NSDate *)date usingFormatter: (NSDateFormatter *)formatter withID:(NSString *)userID

{

    return [[formatter stringFromDate: date] stringByAppendingFormat:@"  userID:%@",userID];

}

- (NSString *)getCurData

{

NSDateFormatter * dateFormat = [[NSDateFormatter alloc] init];

[dateFormat setDateFormat:@"yyyyMMdd hh:mm:ss"];

return [dateFormat stringFromDate:[NSDate date]];

}

@end

下面函数是类的实例方法调用带参数的,

 

 

- (void)instanceMethodCall

{

//performSelector调用

CurrentDate * currentDateClassObject = [[CurrentDate allocinit];

SEL mySelector = @selector(getCurData);

NSString * currentDate = [currentDateClassObject performSelector:mySelector withObject:nil];

NSLog(@"performSelector 调用返回日期:%@",currentDate);

currentDate = nil;

//NSInvocation调用

NSMethodSignature * sig = [[CurrentDate class]instanceMethodSignatureForSelector: mySelector];//得到类和方法的方法信号

NSInvocation * myInvocation = [NSInvocation invocationWithMethodSignature:sig];//调用方法信号

[myInvocation setTarget:currentDateClassObject];// 设置目的实例

[myInvocation setSelector:mySelector];// 设立方法

[myInvocation invoke]; //调用方法

    [myInvocation invoke]; //调用方法

    [myInvocation getReturnValue: &currentDate]; //完成调用设置调用返回值

NSLog(@"NSInvocation 调用返回日期:%@",currentDate);

currentDate = nil;

 

//原始调用多个参数返回数据

    NSDateFormatter * dateFormat = [[NSDateFormatter alloc] init];

[dateFormat setDateFormat:@"yyyyMMdd hh:mm:ss"];

currentDate = [currentDateClassObject stringForDate: [NSDate dateusingFormatter: dateFormatwithID:@"http://blog.sina.com.cn/zing1230"];

    NSLog(@"原始调用设置多个参数返回日期: %@", currentDate);

    currentDate = nil;

 

    //NSInvocation调用多个参数数据

    mySelector = @selector(stringForDate:usingFormatter:withID:); //选择一个方法

    sig = [[CurrentDate class]instanceMethodSignatureForSelector: mySelector]; //得到类和方法的方法信号

    myInvocation = [NSInvocation invocationWithMethodSignature:sig];//调用方法信号

    [myInvocation setTarget: currentDateClassObject];// 设置目的实例

    [myInvocation setSelector: mySelector];// 设立方法

    

    NSDate * myDate = [NSDate date];

    [myInvocation setArgument: &myDate atIndex: 2];//设置参数atIndex的下标必须从2开始。原因为:0 1两个参数已经被target selector占用

    

    NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];

[dateFormatter setDateFormat:@"yyyyMMdd hh:mm:ss"];

    [myInvocation setArgument: &dateFormatter atIndex3];

 

NSString * userID = @"test";

[myInvocation setArgument:&userID atIndex4];

    

[myInvocation retainArguments]; //retain 所有参数,防止参数被释放dealloc   

    [myInvocation invoke]; //调用方法

    [myInvocation getReturnValue: &currentDate]; //完成调用设置调用返回值

 

    NSLog(@"NSInvocation调用设置多个参数返回日期: %@", currentDate);

 

}

参考内容

http://www.cnblogs.com/chenjunbiao/archive/2011/04/20/2022197.html

http://blog.csdn.net/cancer1617/article/details/6855484



转载:http://blog.sina.com.cn/s/blog_677089db01013w48.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值