Question: As part of a unit test framework, I'm writing a function `genArray` that will generate NSArrays populated by a passed in generator block. So `[ObjCheck genArray: genInt]` would generate an NSArray of random integers, `[ObjCheck genArray: genChar]` would generate an NSArray of random characters, etc. In particular, I'm getting compiler errors in my implementation of `genArray` and `genString`, a wrapper around `[ObjCheck genArray: genChar]`.

I believe Objective C can manipulate blocks this dynamically, but I don't have the syntax right.

[ObjCheck.m](https://github.com/mcandre/objcheck/blob/master/ObjCheck.m)

```
+ (id) genArray: (id) gen {
    NSArray* arr = [NSMutableArray array];

    int len = [self genInt] % 100;

    int i;
    for (i = 0; i < len; i++) {
        id value = gen();

        arr = [arr arrayByAddingObject: value];
    }

    return arr;
}

+ (id) genString {
    NSString* s = @"";

    char (^g)() = ^() {
        return [ObjCheck genChar];
    };

    NSArray* arr = [self genArray: g];
    s = [arr componentsJoinedByString: @""];

    return s;
}

```

When I try to compile, gcc complains that it can't do `gen()`, because `gen` is not a function. This makes sense, since `gen` is indeed not a function but an `id` which must be cast to a function.

But when I rewrite the signatures to use `id^()` instead of `id`, I also get compiler errors. Can Objective C handle arbitrarily typed blocks (genArray needs this), or is that too dynamic?

Answer: 
Given that blocks are objects, you can cast between block types and `id` whenever you want, though if you cast the block to the wrong block type and call it, you're going to get unexpected results (since there's no way to dynamically check at runtime what the "real" type of the block is\*).

BTW, `id^()` isn't a type. You're thinking of `id(^)()`. This may be a source of compiler error for you. You should be able to update `+genArray:` to use

```
id value = ((id(^)())(gen))();

```

Naturally, that's pretty ugly.

\*There actually is a way, llvm inserts an obj-c type-encoded string representing the type of the block into the block's internal structure, but this is an implementation detail and would rely on you casting the block to its internal implementation structure in order to extract.
Blocks are a C-level feature, not an ObjC one - you work with them analogously to function pointers. There's [an article with a very concise overview of the syntax](http://thirdcog.eu/pwcblocks/#cblocks). (And most everything else.)

In your example, I'd make the `gen` parameter an `id (^gen)()`. (Or possibly make it return a `void*`, using `id` would imply to me that `gen` generates ObjC objects and not completely arbitrary types.)