看下面的内容,为什么即使b_obj是c_obj也不属于__NSCFBoolean类?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 ?!?
提前致谢!
编辑 :
如果我强制转换为BOOL,则可以解决此问题。
// A cast "fixes" it
id d_obj = @((BOOL)![[NSNumber numberWithInt:1] boolValue]);
NSLog(@"d_obj class: %@", [d_obj class]); // class is __NSCFBoolean
逻辑否定运算符(!
)的类型int
在(Objective-)C中。由于与的类型不同BOOL
,因此编译器对它的包装不同也就不足为奇了。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句