NSBlock iOS的NSMethodSignature(或编码)的(iOS NSMethodSi

2019-08-01 03:18发布

我需要一种方法来检查在运行时给定块的参数和参数类型的数字(我需要这对一些对象映射库我目前正在写,我映射格式字符串值选择,希望同为块) 。

我试图从代码下面的例子,但由于某种原因,它不是为我工作,为字符串描述returnin为零。

你知道的方式在运行时(最好为AFE iPhone应用程序商店提交),以评估块签名?

这是我使用的代码:

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

来源: 检查目标C块类型? https://github.com/mikeash/MABlockForwarding/blob/master/main.m

Answer 1:

使用CTBlockDescription ,你可以得到你需要的所有运行时信息NSMethodSignature对象。 使用方法很简单:

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

这将输出以下特征:

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}


文章来源: iOS NSMethodSignature (or encoding) of NSBlock