instanceMethodForSelector:
解説
セレクタaSelectorのインスタンスメソッドが実装されているアドレスを探して返します。なければエラーとなります。
このメソッドはクラスオブジェクトに対してインスタンスメソッドが実装されているかをチェックする時だけに使用します。
フレームワーク
Foundation
クラス
NSObject
Class Methods
使用可能
10.0
参照
関数へのポインタについての解説は荻原本参照 8章NSObjectクラスとランタイムシステム コラム 関数へのポインタ
例文
#import "MyObject.h" // @implementation MyObject - (IBAction)myAction:(id)sender { BOOL (*test)(id, SEL, id); test = (BOOL (*)(id, SEL, id))[self methodForSelector:@selector(isEqual:)]; ( !test(self, @selector(sel:), sender) ) ? NSLog(@"YES") : NSLog(@"NO"); } -(void)sel:(id)sender { NSLog(@"!!!"); } @end
// // OOOAppDelegate.m // pointerToFunction // // Created by 大森 智史 on 12/01/30. // Copyright (c) 2012年 __MyCompanyName__. All rights reserved. // #import "OOOAppDelegate.h" @implementation OOOAppDelegate @synthesize window = _window; -(id)aFunc:(id)obj1 title:(id)obj2; { NSString *retStr = [NSString stringWithFormat:@"%@+%@",obj1,obj2]; return retStr; } - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { IMP funcp; funcp = [self methodForSelector:@selector(aFunc:title:)]; NSString *param1 = @"1"; NSString *param2 = @"2"; id xyz = (*funcp)(self, @selector(aFunc:title:),param1,param2); NSLog(@"%@",[xyz description]); } @end