使いどころ? なにそれ?
あらゆる実装していない引数なしメソッドでnilを返す。
#import "ClassA.h" #include <objc/runtime.h> @implementation ClassA static NSMethodSignature *sSig = nil; - (NSMethodSignature *)sig { if(sSig) return sSig; Method method = class_getInstanceMethod([self class], @selector(returnNil)); const char *encode = method_getTypeEncoding(method); sSig = [[NSMethodSignature signatureWithObjCTypes:encode] retain]; return sSig; } - (id)returnNil { return nil; } - (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector; { NSString *selName = NSStringFromSelector(aSelector); if(![selName hasSuffix:@":"]) { // 引数なし return [self sig]; } return nil; } - (void)forwardInvocation:(NSInvocation *)anInvocation { NSMethodSignature *sig = [anInvocation methodSignature]; if(sig == [self sig]) { [anInvocation setSelector:@selector(returnNil)]; [anInvocation invokeWithTarget:self]; return; } [self doesNotRecognizeSelector:selector]; } @end
使いどころはある!