可以使用 nil 作为 BOOL 表示 NO 吗?

Okay to use nil as BOOL for NO?

我想取消选择 UITableView 中碰巧突出显示的每个可见单元格。我尝试了这行代码,令我惊讶的是它确实有效。

[[self.tableView visibleCells]makeObjectsPerformSelector:@selector(setHighlighted:)withObject:nil];

所以我尝试了相反的方法,用这个来突出显示每个单元格:

[[self.tableView visibleCells]makeObjectsPerformSelector:@selector(setHighlighted:)withObject:[NSObject new]];

并且有效!这样编码可以吗?要使用对象或 nil 代替布尔值 YES 或 NO?还是会造成什么问题?

[...] I was amazed that it actually works.

这有效是巧合。很大程度上取决于架构的调用约定和对象的实际地址。

例如,如果 [NSObject new] 将 return 对象地址对齐到 256 字节(尾随十六进制数字 00),它可能被解释为 NO

Is it OK to code like this?

绝对不是。正如我所说,它很脆弱,可能会让其他开发者感到困惑。

以下是 C 标准的摘录(关于使用与声明不匹配的参数类型调用函数):

6.5.2.2 Function calls

[...]

9) If the function is defined with a type that is not compatible with the type (of the expression) pointed to by the expression that denotes the called function, the behavior is undefined.