blackchena blackchena - 4 months ago 13
Objective-C Question

I have a class I want some method transfer to an array's object

Like the title said: I have a ObjcClass
I want some thing can be reused ,
because the class may have

-(void)test1:xxx -(void)test2:xxx argu:yyy

I don't want to do that

[dispatchArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
[obj test2:xxx argu:yyy];
}];


example:

- (void)test:(NSString *)argument1 {
NSArray *dispatchArray = @[];//If the array is initialized with multiple objects
//I want each object to call the "test:" method unlike the following
// [dispatchArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
// [obj performSelector:@selector(test:) withObject:argument1];
// // or [obj test:argument1];
// }];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[_services enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL * stop) {
if ([obj respondsToSelector:_cmd]) {
[obj application:application didFinishLaunchingWithOptions:launchOptions];
}
}];

return YES;
}


like this ,UIApplicationDelegate has a number of method ,I don't want write [obj application:application didFinishLaunchingWithOptions:launchOptions]; or [obj applicationWillResignActive:application]; at every method,On the contrary I hope that method like [obj respondsToSelector:_cmd] ,that I can propose as a general method like [obj invokeWithMethod:_cmd arguments:_VA_LIST];
Whether these methods can be optimized,because they do the same thing to different method

Answer

The methods you app delegate has been implemented, you should implement as before. To the method in UIApplicationDelegate protocol which your app delegate did not implement, you can use message forwarding to achieve your target. Override the message forwarding methods of your app delegate as below:

- (BOOL)respondsToSelector:(SEL)aSelector {
    struct objc_method_description desc = protocol_getMethodDescription(objc_getProtocol("UIApplicationDelegate"), aSelector, NO, YES);
    if (desc.name != nil) {
        return YES;
    }
    return [super respondsToSelector:aSelector];
}

- (void)forwardInvocation:(NSInvocation *)anInvocation {
    SEL selector = [anInvocation selector];
    struct objc_method_description desc = protocol_getMethodDescription(objc_getProtocol("UIApplicationDelegate"), selector, NO, YES);
    if (desc.name != nil) {
        [_services enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([obj respondsToSelector:selector]) {
                [anInvocation invokeWithTarget:obj];
            }
        }];
    }
}

Get the return values:

NSMutableArray *returnValues = [NSMutableArray array];
[_services enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
    id returnValue = [NSNull null];
    if ([obj respondsToSelector:selector]) {
        [anInvocation invokeWithTarget:obj];

        const char *returnType = anInvocation.methodSignature.methodReturnType;

        if( !strcmp(returnType, @encode(void)) ){
            //If the return value is `void`, just set returnValue = [NSNull null]
        } else if( !strcmp(returnType, @encode(id)) ){
            // if the return type is derived data types(`id`)
            [anInvocation getReturnValue:&returnValue];
        }else{
            //if the return value is basicdata type
            NSUInteger length = [anInvocation.methodSignature methodReturnLength];
            void *buffer = (void *)malloc(length);
            [anInvocation getReturnValue:buffer];
            if( !strcmp(returnType, @encode(BOOL)) ) {
                returnValue = [NSNumber numberWithBool:*((BOOL*)buffer)];  
            } else if( !strcmp(returnType, @encode(NSInteger)) ){
                returnValue = [NSNumber numberWithInteger:*((NSInteger*)buffer)];  
            }  
            returnValue = [NSValue valueWithBytes:buffer objCType:returnType];
        }
    }
    // If the `obj` can not responds to selector, or the return value is void(nil), we set the `returnValue = [NSNull null]`
    [returnValues addObject:returnValue];
}]
Comments