ios - NSMethodSignature 的 BOOL 编码错误

标签 ios objective-c ios7 ios8

我在使用 [NSMethodSignature getArgumentTypeAtIndex] 函数时遇到了非常奇怪的行为。它返回 BOOL 类型的“@”字符,根据 objective-c type encodings 错误.如果我使用 objc\runtime.h 库方法 method_getTypeEncoding BOOL 类型正确表示为“B”,但我不明白为什么它不能与更高级别的 NSMethodSignature 一起使用。以下代码演示了该问题:

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    NSInvocation* inv = [NSInvocation invocationWithMethodSignature:[self methodSignatureForSelector:@selector(viewDidAppear:)]];
    const char* encFromGetArgument = [[inv methodSignature] getArgumentTypeAtIndex:2];

    const char* encFromMethodSignature = method_getTypeEncoding(class_getInstanceMethod([self class], @selector(viewDidAppear:)));;
    const char* methodEncodingPure = [[[[NSString stringWithUTF8String:encFromMethodSignature] componentsSeparatedByCharactersInSet:[NSCharacterSet decimalDigitCharacterSet]] componentsJoinedByString:@""] UTF8String];//remove stack sizes


    NSLog(@"BOOL arg from NSMethodSignature: %s", encFromGetArgument);
    NSLog(@"BOOL arg from objc/runtime.h: %c", methodEncodingPure[3]);//first type is for return, second is target, third is selector
}

令人惊讶的是(至少对于男性而言)上面打印了以下内容:

BOOL arg from NSMethodSignature: @

BOOL arg from objc/runtime.h: B

我目前正在使用我自己的实现来避免这种奇怪的行为,但是我想知道我是否遗漏了什么或者它只是一个错误。我唯一的线索是 BOOL 是原始的,因此在调用 objective-c 方法时不能直接使用它。但是,当我尝试检查它时 [object isKingOfClass:[NSNumber class]] 返回 NO。

更新

好的,我已将 XCode 更新到最新版本 (6.1 6A1052d),情况大大改善。但是我现在的问题是区分 unsigned char 编码和真正的 bool 编码。我知道在旧版本中 BOOL 是作为 char 的 typedef,但是我如何才能完成真正的 char 与 BOOL 编码?我现在的结果是:

对于Simulator iPhone6和真机iPhone6我收到了:

argument 2: -------- -------- -------- --------
        type encoding (B) 'B'
        flags {}

BOOL arg from NSMethodSignature: B
BOOL arg from objc/runtime.h: B

这很棒,但是对于模拟器 iPhone4s 和真实设备 iPhone5 我得到:

argument 2: -------- -------- -------- --------
        type encoding (c) 'c'
        flags {isSigned}

BOOL arg from NSMethodSignature: c
BOOL arg from objc/runtime.h: c

我很确定,如果我检查 iPhone5s,它会得到与 iPhone6 相同的输出(因为我认为这都是关于 64 位架构的)。所以我现在的问题是如何正确解决旧设备,如何为它们区分 BOOL 的字符?或者我是否应该假设如果编码是“c”并且参数等于“1”我们有 YES,而对于“0”我们没有?

最佳答案

char *buf1 = @encode(BOOL);
NSLog(@"bool type is: %s", buf1);

在 32 位模拟器上,encode(BOOL) 返回“c”,而在 64 位模拟器上,它返回“B”。在 Build Settings 中,将 Architectures 更改为 $(ARCHS_STANDARD_32_BIT),然后它将为您返回“c”。

在 objc/objc.h 文件中。

#define OBJC_BOOL_DEFINED

/// Type to represent a boolean value.
#if !defined(OBJC_HIDE_64) && TARGET_OS_IPHONE && __LP64__
typedef bool BOOL;
#else
typedef signed char BOOL; 
// BOOL is explicitly signed so @encode(BOOL) == "c" rather than "C" 
// even if -funsigned-char is used.
#endif

BOOL 在 32 位中是 typeof signed char。您可以将它与 unsigned char 区分开来。

@encode(char) --> 'c'
@encode(unsigned char) --> 'C'

您可以从 here 判断设备是 32 位还是 64 位, 如果是 32 位, 'c' 对 BOOL 校验有效。

关于ios - NSMethodSignature 的 BOOL 编码错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26459754/

相关文章:

iphone - 如何找到任何地方的经纬度和时区?

iOS >> 自动布局 >> iPhone 4 与 iPhone 5 >> 设置多个对象来改变大小

ios7 - 如何在 iOS 7 中专门为 cocos2D 隐藏导航栏?

ios7 - 类似 IOS 7 天气 APP 的过渡/动画

ios - 从模态(objective-c)引用呈现 View Controller

iphone UIView - 加载 View 时收到通知?

ios - 无法正确地为父 View 中的 subview 的框架设置动画

objective-c - 检测删除键被按下

objective-c - EGO照片查看器 : gesture not working

iOS 6 和 7 UINavigationBar 间距