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 8077827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:35:48+00:00 2026-06-05T15:35:48+00:00

This is primarily a curiosity, I’m not really sure what’s the practical use of

  • 0

This is primarily a curiosity, I’m not really sure what’s the practical use of this but here goes.

Since blocks are also Objective-C objects, is it possible to check their type? That is, does it respond to the isKindOfClass: message and how to use that message with respect to blocks?

My naive thought that it’s probably like this:

-(void) aMethod {
    typedef int (^BlockA)(int x, int y);
    id blockVar = ...; // get a block from somewhere
    if([blockVar isKindOfClass:BlockA]) {
        BlockA blockVarA = blockVar;
        int result = blockVarA(1,2);
    }
}

The code above probably won’t work. But if it is possible to check a block’s type, what is the correct way to do it?

  • 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-06-05T15:35:50+00:00Added an answer on June 5, 2026 at 3:35 pm

    Can do, kinda sorta.

    But first, let’s disambiguate. -[NSObject isKindOfClass:] can tell you it’s a block, and that’s about it. E.g. I believe this line of code — ostensibly & unfortunately A BAD IDEA — will return YES for blocks on present Lion & iOS 5.x:

    [myBlock isKindOfClass:NSClassFromString(@"NSBlock")]
    

    That won’t help you distinguish the block’s function signature.

    But it can be done, by snagging the signature from the block’s documented internal struct. Code follows for an example OS X command-line app, much of which ripped from Mike Ash’s MABlockClosure (great detailed explanation). (UPDATE: Github project CTObjectiveCRuntimeAdditions also apparently provides library code for just this purpose.)

    #import <Foundation/Foundation.h>
    
    struct BlockDescriptor {
        unsigned long reserved;
        unsigned long size;
        void *rest[1];
    };
    
    struct Block {
        void *isa;
        int flags;
        int reserved;
        void *invoke;
        struct BlockDescriptor *descriptor;
    };
    
    static const char *BlockSig(id blockObj)
    {
        struct Block *block = (void *)blockObj;
        struct BlockDescriptor *descriptor = block->descriptor;
    
        int copyDisposeFlag = 1 << 25;
        int signatureFlag = 1 << 30;
    
        assert(block->flags & signatureFlag);
    
        int index = 0;
        if(block->flags & copyDisposeFlag)
            index += 2;
    
        return descriptor->rest[index];
    }
    
    int main(int argc, const char * argv[])
    {
        @autoreleasepool {
    
            int (^block)(NSNumber *) = ^(NSNumber *num) { 
                NSLog(@"%@ %@", NSStringFromClass([num class]), num); 
                return [num intValue]; 
            };
            NSLog(@"signature %s", BlockSig(block));
            NSLog(@"retval %d", (int)block([NSNumber numberWithInt:42]));
        }
        return 0;
    }
    

    Run this and you should get something like:

    [58003:403] signature i16@?0@8
    [58003:403] __NSCFNumber 42
    [58003:403] retval 42
    

    The numbers in the signature (I’m told they are offsets) can be stripped for simpler i@?@.

    The signature is in the @encode format, which isn’t perfect (e.g. most objects map to same @), but should afford you some ability to distinguish blocks with different signatures at runtime.

    While it’s not documented in the Apple link, my testing points to @? being the code for a block type, which makes sense of the signature above. I found a clang-developers discussion on this issue which seems to back this up.

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

Sidebar

Related Questions

This is a question of curiosity about accepted coding practices. I'm (primarily) a Java
I would like to actually use this wrapper, but the problem is i don't
I am using WCF RIA services with silverlight. Obviously I am using this primarily
This is primarily a question of possibilities more than instructions. I'm a programming consultant
SQL Server provides the type [rowguid]. I like to use this as unique primary
This has probably been answer already but I am trying to return the primary
create table ImagenesUsuario { idImagen int primary key not null IDENTITY } This doesn't
Okay, this is (probably) a very simple question, but I am afraid I know
Primarily this seems to be a technique used by games, where they have all
The focus of this question is primarily to develop a relational db scheme given

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.