IOS NSMethodSignature (or encoding) NSBlock

I need a way to check the number of arguments and argument types of a given block at runtime (I need this for some object mapping library that I am writing now, I map string formatted values ​​for selectors, I want for blocks).

I tried the code from the examples below, but for some reason it does not work for me and does not return a string for the description.

Do you know a way to evaluate block signatures at runtime (preferably, for example, for iPhone App Store apps)?

This is the code I'm using:

struct BlockDescriptor { unsigned long reserved; unsigned long size; void *rest[1]; }; struct Block { void *isa; int flags; int reserved; void (*invoke)(struct __block_literal_1 *); struct BlockDescriptor *descriptor; }; enum { BLOCK_HAS_COPY_DISPOSE = (1 << 25), BLOCK_HAS_CTOR = (1 << 26), // helpers have C++ code BLOCK_IS_GLOBAL = (1 << 28), BLOCK_HAS_STRET = (1 << 29), // IFF BLOCK_HAS_SIGNATURE BLOCK_HAS_SIGNATURE = (1 << 30), }; static const char *BlockSig(id blockObj) { struct Block *block = (void *)blockObj; struct BlockDescriptor *descriptor = block->descriptor; assert(block->flags & BLOCK_HAS_SIGNATURE); int index = 0; if(block->flags & BLOCK_HAS_COPY_DISPOSE) index += 2; return descriptor->rest[index]; } -(NSString*)signatureForBlock:(id)block { NSString* sig = [NSString stringWithUTF8String:BlockSig(block)]; sig = [sig substringFromIndex:1]; // remove c NSArray* components = [sig componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@"0123456789?"]]; sig = [components componentsJoinedByString:@""]; return sig; } 

Then do:

 NSString * (^block)(int, NSArray *) = ^NSString * (int i, NSArray * a){ return @"Oh, yeah!"; }; NSLog(@"signature %s", BlockSig(block)); // ==> this returns null 

Source: Objective-C block type check? https://github.com/mikeash/MABlockForwarding/blob/master/main.m

+4
source share
1 answer

Using CTBlockDescription , you can get all the necessary runtime information in the form of NSMethodSignature . The use is simple:

 NSString * (^block)(int, NSArray *) = ^NSString * (int i, NSArray * a){ return @"Oh, yeah!"; }; NSMethodSignature *signature = [[[CTBlockDescription alloc] initWithBlock:block] blockSignature]; NSLog(@"signature %@", [signature debugDescription]); 

The following signature will be displayed here:

 signature <NSMethodSignature: 0x6844900> number of arguments = 3 frame size = 12 is special struct return? NO return value: -------- -------- -------- -------- type encoding (@) '@' flags {isObject} modifiers {} frame {offset = 0, offset adjust = 0, size = 4, size adjust = 0} memory {offset = 0, size = 4} argument 0: -------- -------- -------- -------- type encoding (@) '@?' flags {isObject} modifiers {} frame {offset = 0, offset adjust = 0, size = 4, size adjust = 0} memory {offset = 0, size = 4} argument 1: -------- -------- -------- -------- type encoding (i) 'i' flags {isSigned} modifiers {} frame {offset = 4, offset adjust = 0, size = 4, size adjust = 0} memory {offset = 0, size = 4} argument 2: -------- -------- -------- -------- type encoding (@) '@' flags {isObject} modifiers {} frame {offset = 8, offset adjust = 0, size = 4, size adjust = 0} memory {offset = 0, size = 4} 
+5
source

Source: https://habr.com/ru/post/1437669/


All Articles