NSObject 이유 NSInvocation 호출 방법

2235 단어
NSObject+Extension.h
#import 

@interface NSObject (Extension)
- (id)performSelector:(SEL)selector withObjects:(NSArray *)objects;
@end
NSObject+Extension.m
#import "NSObject+Extension.h"

@implementation NSObject (Extension)

- (id)performSelector:(SEL)selector withObjects:(NSArray *)objects{
    
    //  ( )
    NSMethodSignature *signature = [[self class] instanceMethodSignatureForSelector:selector];
    
    if (signature == nil) {
        // @throw [NSException exceptionWithName:@" " reason:@" " userInfo:nil];
        [NSException raise:@" " format:@"%@ ", NSStringFromSelector(selector)];
    }
    
    // NSInvocation :  NSInvocation ( 、 、 、 )
    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
    invocation.target = self;
    invocation.selector = selector;
    
    //  
    NSInteger paramsCount = signature.numberOfArguments - 2; //  self、_cmd 
    paramsCount = MIN(paramsCount, objects.count);
    for (NSInteger i = 0; i < paramsCount; i++) {
        id object = objects[i];
        if ([object isKindOfClass:[NSNull class]]) continue;
        [invocation setArgument:&object atIndex:i + 2];
    }
    
    //  
    [invocation invoke];
    
    //  
    id returnValue = nil;
    if (signature.methodReturnLength) { //  , 
        [invocation getReturnValue:&returnValue];
    }
    
    return returnValue;
}

@end
AppDelegate.m에서 이상 포착 가능
@implementation AppDelegate

/**
 *  
 * 1. 
 * 2.Flurry
 * 3.Crashlytics
 */

/**
 *  
 */
void handleException(NSException *exception){
    NSMutableDictionary *info = [NSMutableDictionary dictionary];
    info[@"callStack"] = [exception callStackSymbols]; //  ( )
    info[@"name"] = [exception name]; //  
    info[@"reason"] = [exception reason]; //  ( )
    // [info writeToFile: atomically:];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    //  
    //  
    NSSetUncaughtExceptionHandler(handleException);
    
    return YES;
}

@end

좋은 웹페이지 즐겨찾기