ios prevent application crashes reason: '- [Person eat]: unrecognized selector sent to instance

Create a test class Person

Person.h file


#import <Foundation/Foundation.h>

@interface Person : NSObject

- (void)eat;
@end

Person.m file


#import "Person.h"

@implementation Person

+ (BOOL)resolveInstanceMethod:(SEL)sel {
    return [super resolveInstanceMethod:sel];
}

+ (BOOL)resolveClassMethod:(SEL)sel {
    return [super resolveClassMethod:sel];
}

- (id)forwardingTargetForSelector:(SEL)aSelector {
    return [super forwardingTargetForSelector:aSelector];
}

- (void)forwardInvocation:(NSInvocation *)anInvocation {

}
- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {
    NSString *sel = NSStringFromSelector(aSelector);
    if ([sel rangeOfString:@"set"].location == 0) {
        //动态造一个 setter函数
        return [NSMethodSignature signatureWithObjCTypes:"v@:@"];
    } else {
        //动态造一个 getter函数
        return [NSMethodSignature signatureWithObjCTypes:"@@:"];
    }
    return [super methodSignatureForSelector:aSelector];
}

@end

Specific for further improvement;

https://blog.csdn.net/hanhailong18/article/details/70909632

Published 172 original articles · won praise 35 · views 390 000 +

Guess you like

Origin blog.csdn.net/u012198553/article/details/80252526