In objective-c, we have -forwardInvocation: this can be used as follows:
-(void)forwardInvocation:(NSInvocation*) anInvocation{ BOOL didForward = NO; //iterate over our proxied objects for(id proxyObject in self.proxyObjects){ //invoke the with the proxy object if it can handle the selector if ([proxyObject respondsToSelector:[anInvocation selector]]){ didForward = YES; [anInvocation invokeWithTarget: proxyObject]; } } //if we did not forward the invocation, then call super if(!didForward){ [super forwardInvocation: anInvocation]; } }
This is useful if you have a group of specific classes for which everyone needs the same messages. For example, if you use several analytics platforms, each of which needs the same messages, but will process them in different ways.
Lets do it quickly, given what we know about the language. It starts off simply:
func doSomething1(){ for proxyObject in proxyObjects{ proxyObject.doSomething1() } }
But then it repeats:
func doSomething2(){ for proxyObject in proxyObjects{ proxyObject.doSomething2() } } func doSomething3(){ for proxyObject in proxyObjects{ proxyObject.doSomething3() } } func doSomething4(){ for proxyObject in proxyObjects{ proxyObject.doSomething4() } } ....And on and on
I know that I can use NSObject in swift, but it's just confusion in objective-c where we need it. What is a more efficient and less surefire way to deal with this in a clean fast?
source share