I'm in the process of learning Objective-C and I'm trying to store method references in an array where I can loop through the array and invoke the methods. I'm coming from an interpreted language background and in a language like Ruby you could do something like the following
method_arr = [objectOne.method(:methodOne), objectTwo.method(:methodOne)]
method_arr.each do |m|
m.call
end
The closest thing I found was using NSInvocation to achieve this with the following
NSMethodSignature *signature = [objectOne methodSignatureForSelector:@selector(methodOne)];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
[invocation setSelector:@selector(methodOne)];
[invocation setTarget:objectOne];
NSArray *methodArr = @[invocation];
for (int i = 0; i < [methodArr count]; i++) {
[methodArr[i] invoke];
}
Another way I found would be to use blocks which isn't referencing the methods but achieves similar results
NSArray *methodArr = @[^{
[objectOne methodOne]
}];
NSInvocation seems a bit tedious for me and was wondering if there's a better way to do this.