Để xử lý "không được công nhận selector" ngoại lệ, chúng ta nên ghi đè lên hai phương pháp:
- (void)forwardInvocation:(NSInvocation *)anInvocation;
- (NSMethodSignature*)methodSignatureForSelector:(SEL)selector;
Trong trường hợp này, nếu chúng ta muốn NSNull để thực hiện các phương pháp NSSString nếu "chọn không được công nhận" ngoại lệ xảy ra, chúng ta nên làm này:
@interface NSNull (InternalNullExtention)
@end
@implementation NSNull (InternalNullExtention)
- (NSMethodSignature*)methodSignatureForSelector:(SEL)selector
{
NSMethodSignature* signature = [super methodSignatureForSelector:selector];
if (!signature) {
signature = [@"" methodSignatureForSelector:selector];
}
return signature;
}
- (void)forwardInvocation:(NSInvocation *)anInvocation
{
SEL aSelector = [anInvocation selector];
if ([@"" respondsToSelector:aSelector])
[anInvocation invokeWithTarget:@""];
else
[self doesNotRecognizeSelector:aSelector];
}
@end
Cảm ơn! Tôi nhận thấy bên cạnh - (void) forwardInvocation: (NSInvocation *) anInvocation, tôi cũng cần phải thực hiện - (NSMethodSignature *) methodSignatureForSelector: (SEL) chọn – Hang