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

  • SEARCH
  • Home
  • 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 6562557
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:43:24+00:00 2026-05-25T13:43:24+00:00

This is an extension of this queston: Is it possible to create a category

  • 0

This is an extension of this queston:
Is it possible to create a category of the "Block" object in Objective-C.

Basically while it seems possible to create a category on blocks, either through NSObject or NSBlock, I’m having trouble understanding how the block would be able to evaluate itself. The example given in the answer to the last question:

- (void) doFoo {
  //do something awesome with self, a block
  //however, you can't do "self()".  
  //You'll have to cast it to a block-type variable and use that
}

Implies that it is possible to somehow cast self to a block variable, but how would one execute the block itself? For example, say I did a category on NSBlock and in a method did:

NSBlock* selfAsBlock = (NSBlock*)self;

Is there any message I can send to selfAsBlock to have the block evaluate?

  • 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-25T13:43:25+00:00Added an answer on May 25, 2026 at 1:43 pm

    Implies that it is possible to somehow cast self to a block variable

    Like this:

    - (void)doFoo {
        // Assume the block receives an int, returns an int,
        // and cast self to the corresponding block type
        int (^selfBlock)(int) = (int (^)(int))self;
    
        // Call itself and print the return value
        printf("in doFoo: %d\n", selfBlock(42));
    }
    

    Note that (in most cases) you need to fix the block signature so that the compiler is able to set up the call site according to the target platform ABI. In the example above, the signature is return type int, single parameter of type int.

    A full example is:

    #import <Foundation/Foundation.h>
    #import <objc/runtime.h>
    
    @interface Foo : NSObject
    - (void)doFoo;
    @end
    
    @implementation Foo
    - (void)doFoo {
        // Assume the block receives an int, returns an int,
        // and cast self to the corresponding block type
        int (^selfBlock)(int) = (int (^)(int))self;
    
        // Call itself and print the return value
        printf("in doFoo: %d\n", selfBlock(42));
    }
    @end
    
    int main(void) {
        [NSAutoreleasePool new];
    
        // From Dave's answer
        Method m = class_getInstanceMethod([Foo class], @selector(doFoo));
        IMP doFoo = method_getImplementation(m);
        const char *type = method_getTypeEncoding(m);
        Class nsblock = NSClassFromString(@"NSBlock");
        class_addMethod(nsblock, @selector(doFoo), doFoo, type);
    
        // A block that receives an int, returns an int
        int (^doubler)(int) = ^int(int someNumber){ return someNumber + someNumber; };
    
        // Call the category method which in turn calls itself (the block)
        [doubler doFoo];
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is an extension of question: Order of the initialization in Java so in
This is an extension of the question I asked here I have a relationship
This is an extension of a question I previously asked here . Long story
This is an extension to a question which I asked some time ago. I
This is an extension for this question asked an hour ago. We cannot modify
This is an extension of this question and probably might even be a duplicate
This is an extension to the question I asked here: Get text from clipboard
This is an extension of this question: SQLite problem selecting two columns as one
As an extension to this question here Linking JavaScript Libraries in User Controls I
This is something of an extension to this question: Dispatching to correct function with

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.