Objective c 为什么这两个是";“自动装箱”;给出不同结果的陈述?

Objective c 为什么这两个是";“自动装箱”;给出不同结果的陈述?,objective-c,Objective C,看看下面的例子,为什么c_obj不属于c_nscfboolian类,即使b_obj属于c_nscfboolian类?b和c之间唯一的区别是否定运算符。我 BOOL a = ![[NSNumber numberWithInt:1] boolValue]; id a_obj = @(a); NSLog(@"a_obj class: %@", [a_obj class]); // class is __NSCFBoolean // This one behaves the same as a id

看看下面的例子,为什么c_obj不属于c_nscfboolian类,即使b_obj属于c_nscfboolian类?b和c之间唯一的区别是否定运算符。我
BOOL a = ![[NSNumber numberWithInt:1] boolValue];
id a_obj = @(a);
NSLog(@"a_obj class: %@", [a_obj class]); // class is __NSCFBoolean

// This one behaves the same as a
id b_obj = @([[NSNumber numberWithInt:1] boolValue]);
NSLog(@"b_obj class: %@", [b_obj class]); // class is __NSCFBoolean

// But not this one, even though it is pretty much the same
id c_obj = @(![[NSNumber numberWithInt:1] boolValue]);
NSLog(@"c_obj class: %@", [c_obj class]); // class is __NSCFNumber ?!?
提前谢谢

编辑:

如果我向布尔施法,它会解决问题

// A cast "fixes" it
id d_obj = @((BOOL)![[NSNumber numberWithInt:1] boolValue]);
NSLog(@"d_obj class: %@", [d_obj class]); // class is __NSCFBoolean

逻辑求反运算符(
)的类型是(Objective-)C中的
int
。由于这是一种不同于
BOOL
的类型,因此编译器对其进行不同的装箱也就不足为奇了。

我无法回答您的问题,但我可以告诉您,您不应该真的在意。在处理
NSNumber
s时,您根本没有任何保证。从文档中可以看出:“请注意,数字对象不一定保留它们创建时使用的类型”。而
@()
不过是
[NSNumber number number with…]
的简写!我感到困惑的原因是,当将字典序列化为json时,当值为true时,我会在字符串中得到“yes”,当值为false时,我会在字符串中得到0,这让我非常困惑。Ken Thomases的答案很明显,否定运算符返回一个int;当不强制转换时,会自动装箱到NSCFN编号。。。