观察以下内容,为什么c_obj不是类__NSCFBoolean,即使b_obj是? 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
答案 0 :(得分:4)
逻辑否定运算符(!
)的类型在(Objective-)C中为int
。由于这与BOOL
的类型不同,因此编译器对其进行不同的处理并不是特别令人惊讶。