I want to make an array of functions so that I could randomize the order in which these arrays happen. Could I do something like:
NSMutableArray *MyArray = [NSMutableArray arrayWithObjects: function1, function2, function3, nil];
So that if I could do something like this;
RandomNum = arc4random() %([MyArray count]);
MyArray[RandomNum];
Thus Randomizing the order in which these functions are called in? How do I store the functions in this array?
As for ObjC blocks you can just store them in an NSArray directly. Plain C functions have to be wrapped into an NSValue:
NSArray *functions = @[[NSValue valueWithPointer:function1], [NSValue valueWithPointer:function2]];
Then you can call them as follow, just make sure you cast it to correct signature:
((void (*)(int))[functions[RandomNum] pointerValue])(10);
                        There ways come to mind
sample 1 -- blocks
    NSMutableArray *array = [NSMutableArray array];
    [array addObject:^{ /* block */ }];
sample 2 -- invocation
    NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[object methodSignatureForSelector:selector]];
    invocation.target = object;
    invocation.selector = selector;
    [array addObject:invocation];
sample 3 -- C-Function
    [NSValue valueWithPointer:function1]
                        Store a pointer to a function in an array and call the function:
#import <Foundation/Foundation.h>
static void hello() {
    NSLog(@"hey\n");
}
int main(int argc, char *argv[]) {
    @autoreleasepool {
        NSPointerArray *array = [NSPointerArray pointerArrayWithOptions:NSPointerFunctionsOpaqueMemory];
        [array addPointer: &hello];
        void* ptr = [array pointerAtIndex:0];
        ((void (*)(void)) ptr)(); // prints hey
    }
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With