- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我需要一种方法来在运行时检查给定 block 的参数数量和参数类型(我目前正在编写的某些对象映射库需要这个,我正在将字符串格式的值映射到选择器,想要相同的 block )。
我尝试了以下示例中的代码,但出于某种原因它对我不起作用,并为字符串描述返回 nil。
您是否知道一种在运行时评估 block 签名的方法(最好是对 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
来源: Checking Objective-C block type? https://github.com/mikeash/MABlockForwarding/blob/master/main.m
最佳答案
使用 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}
关于NSBlock 的 iOS NSMethodSignature(或编码),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12715586/
我有 View Controller 1 比方说 VC1,我正在插入我的另一个 View Controller 比方说 VC2。 在 VC1 中,我的 block 类型定义如下 #define typ
在 View Controller 中,我有一个复制类型的 NSBlock 属性,如下所示 @interface myViewController @property(nonatomic, copy)
我将通过声明我将要问的内容仅用于教育和可能的调试目的来作为这个问题的序言。 如何在 Objective C 运行时内部创建 block 对象? 我看到了所有代表各种 block 类型的类的层次结构,层
我需要一种方法来在运行时检查给定 block 的参数数量和参数类型(我目前正在编写的某些对象映射库需要这个,我正在将字符串格式的值映射到选择器,想要相同的 block )。 我尝试了以下示例中的代码,
- (void) addABlock { void (^aBlock)(void) = ^() { [someObject doSomething]; }; [self.myMutableArray
我是一名优秀的程序员,十分优秀!