我有一个类,我想要一些方法转移到数组的对象

时间:2016-07-29 02:29:35

标签: ios objective-c message-forwarding

像标题一样说:我有一个ObjcClass 我希望有些东西可以重复使用, 因为班级可能有 -(void)test1:xxx -(void)test2:xxx argu:yyy 我不想这样做

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

示例:

 - (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;
}
像这样,UIApplicationDelegate有很多方法,我不想写 [obj应用程序:application didFinishLaunchingWithOptions:launchOptions]; [obj applicationWillResignActive:application]; 在每个方法中,相反,我希望像 [obj respondsToSelector:_cmd] 这样的方法,我可以建议像 [obj invokeWithMethod:_cmd arguments:_VA_LIST]; < /强> Whether these methods can be optimized,because they do the same thing to different method

2 个答案:

答案 0 :(得分:1)

您实施的app方法已经实施,您应该像以前一样实施。对于您的app委托未实现的UIApplicationDelegate协议中的方法,您可以使用消息转发来实现目标。覆盖应用代理的邮件转发方法,如下所示:

- (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];
            }
        }];
    }
}

获取返回值:

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];
}]

答案 1 :(得分:0)

看起来你只想循环遍历数组中的对象。这不是很安全的类型。所有对象必须提供&#34;测试&#34;方法。如果他们都是比使用NSObject更好的Class。

for (NSObject *obj in dispatchArray) {
        [obj performSelector:@selector(test:) withObject:argument1];
}