Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6684257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:55:09+00:00 2026-05-26T04:55:09+00:00

As part of a unit test framework, I’m writing a function genArray that will

  • 0

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

+ (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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T04:55:10+00:00Added an answer on May 26, 2026 at 4:55 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for a javascript unit test framework that I can use as part
I would like to perform an audit as part of a unit test that
Today I ran XtUnit at a part of my unit testing framework to to
As part of the base class for some extensive unit testing, I am writing
I've created a unit test that tests interactions on my ViewModel class in a
Im trying to unit test a class in some Java code that I've inherited.
I am using silverlight unit tests to test an important part of our site.
I have an odd problem. I have a unit test that keeps getting stuck
As part of a unit test I am trying to mock the return value
I was writing some Unit tests last week for a piece of code that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.