6

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?

Neil Shweky
  • 893
  • 2
  • 10
  • 23
  • 2
    Please specify the language you're using. Also add it to your post's tags. – Tomáš Zato Feb 09 '14 at 14:19
  • And do you mean c functions, blocks, or selectors for methods? – Max Feb 09 '14 at 14:23
  • Have a look at Objective-C blocks: http://stackoverflow.com/questions/7997666/storing-blocks-in-an-array and NSInvocation: http://stackoverflow.com/questions/313400/nsinvocation-for-dummies – Atomix Feb 09 '14 at 14:29

4 Answers4

4

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);
Bartosz Ciechanowski
  • 10,293
  • 5
  • 45
  • 60
4

There ways come to mind

  1. you can store blocks in an array
  2. but you can also method invocations there if you want to call methods and not blocks (you would do the latter via NSInvocation)
  3. For C functions, wrap them in an NSValue to store them and call em later

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]
Daij-Djan
  • 49,552
  • 17
  • 113
  • 135
1

Blocks will be the best approach, but I you are not familiar with blocks or from ant other reason you can use: NSSelectorFromString & NSStringFromSelector.

EDITED

  1. Store the methods in array with NSStringFromSelector
  2. You can then shuffle the array as described here : Shuffling array
  3. call the functions using NSSelectorFromString.

For Example

NSArray * functions = @[NSStringFromSelector(selector1),NSStringFromSelector(selector2),
NSStringFromSelector(selector3),NSStringFromSelector(selector4)];

//shuffle the array (see link)
 [functions shuffle];

 [arr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
       [self performSelector:NSSelectorFromString(functions[idx]) withObject:nil];
 }];

(The code was written on the fly, please check it out).

Community
  • 1
  • 1
shannoga
  • 19,649
  • 20
  • 104
  • 169
1

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
    }
}
Jano
  • 62,815
  • 21
  • 164
  • 192