我需要一种在运行时检查给定块的参数数量和参数类型的方法(我需要这个用于我当前正在编写的一些对象映射库,我正在将字符串格式的值映射到选择器,块也需要相同) .
我尝试了以下示例中的代码,但由于某种原因,它对我不起作用,并且为字符串描述返回 nil。
你知道在运行时评估块签名的方法吗(最好是 iPhone 应用商店提交的 afe)?
这是我使用的代码:
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;
}
然后做:
NSString * (^block)(int, NSArray *) = ^NSString * (int i, NSArray * a){
return @"Oh, yeah!";
};
NSLog(@"signature %s", BlockSig(block)); // ==> this returns null
资料来源: 检查 Objective-C 块类型? https://github.com/mikeash/MABlockForwarding/blob/master/main.m