macOS/iOS API解説

iOS , Mac アプリケーション開発のために使われる主要フレームワークの日本語情報です。2010年代に書かれた内容です。今後更新はありません。

目次

invocationWithMethodSignature:

INDEX>Foundation>NSInvocation

メソッドシグネチャで呼び出しオブジェクト(NSInvocation)を作って返します
+(NSInvocation *)invocationWithMethodSignature:(NSMethodSignature *)signature:

解説

メソッドシグネチャで呼び出しオブジェクト(NSInvocation)を作って返します。
呼び出しオブジェクト(NSInvocation)を起動する前に、アクションセレクタとターゲット、引数をセットしておきます。
メソッドシグネチャとは、引数と返り値のセット
たとえば-(BOOL)updateAppointmentsForDate:(Date *)aDateというメソッドの場合
メソッドシグネチャはBOOLとDate *

返り値

( NSInvocation * )

起動オブジェクト

引数

( NSMethodSignature * )signature

メソッドシグネチャ

クラス

NSInvocation

Class Methods

使用可能

10.0
iOS2.0

参照

例文

#import "MyObject.h"
@implementation MyObject

NSTimer *timer=nil;
NSInvocation * invocation ;//起動オブジェクト
- (IBAction)myAction:(id)sender
{
//呼び出すメソッドをコロコロ変えるサンプル
    NSMethodSignature * aSignature ;//メソッドシグネチャ    
    SEL aSelector  = @selector( testSelector1: );//セレクタをセット	
    aSignature = [ self methodSignatureForSelector:aSelector ];//セレクタのシグネチャをセット
	
    invocation = [ NSInvocation invocationWithMethodSignature:aSignature ];//起動オブジェクトをセット
	
	NSLog([aSignature description]);
	
	//起動オブジェクトにターゲットと引数をセットする
    [ invocation setTarget: self ];//ターゲットはself
    [ invocation setSelector: aSelector ];//セレクタをセット
    [ invocation invoke ];//起動する

	
    

}

-(int) testSelector1:(NSString *)string
{
NSLog(@"...call testSelector1");

	//やっぱり testSelector2:メソッドを呼び出すことにする
	[invocation setSelector:@selector( testSelector2: )];
	
	//タイマーで1秒後にinvocationオブジェクを起動
	timer = [NSTimer scheduledTimerWithTimeInterval:1
							invocation:	invocation
								repeats:NO];
	return 1;
}
-(int) testSelector2:(NSString *)string
{
NSLog(@"...call testSelector2");
	
	//やっぱり testSelector1:メソッドを呼び出すことにする
	[invocation setSelector:@selector( testSelector1: )];
	
	//タイマーで1秒後にinvocationオブジェクを起動
	timer = [NSTimer scheduledTimerWithTimeInterval:1
							invocation:	invocation
								repeats:NO];
	return 2;
}
@end