布尔值中的 null 与 false 有何不同?

发布于 2024-12-26 12:41:45 字数 873 浏览 4 评论 0原文

我有一种构建发布请求的方法。该请求可能有 x 个参数,但没有一个是强制性的。有布尔类型的参数(这些参数作为值为“true”或“false”的字符串传递(不是我的决定,它是一个 API)。

问题在于。我的方法声明包含作为问题是,如果布尔参数为 true、为 false 或不存在,则请求会返回不同的结果。

My method id 类似

-(void) createRequestWithID:(NSString *)id translate(NSNumber *)translate onlyLastVersions:(NSNumber *)onlyLastVersions

My method 是由其他开发人员使用和调用的框架的一部分。这意味着,我无法控制它们传递给参数的内容例如,translate 是一个布尔值(我使用 NSNumber 作为包装器),

因此我需要能够确定该方法是否被调用为 true、false 或 nil。请求中不会出现translate="true"、translate="false" 或translate 的情况,

来区分true、false 和nil。

我需要一种方法 获取:

(gdb) p翻译FALSE $5 = (NSNumber *) 0x0

(gdb) p 翻译NIL $6 = (NSNumber *) 0x0

(gdb) po 翻译FALSE 无法打印 NIL 对象的描述。

(gdb) po 翻译NIL 无法打印 NIL 对象的描述。

(gdb) po [翻译FALSE 类] 无法打印 NIL 对象的描述。

(gdb) po [翻译NIL类] 无法打印 NIL 对象的描述。

所以我认为没有办法做到这一点。

I have a method to construct a post request. That request may have x number of parameters but none of them are mandatory. There are parameters of type boolean (those are passed as strings with values "true" or "false" (not my decision, it is an API).

Here is the problem. My method declaration contains all the parameters that are accepted as part of the post request. The problem is that the request returns different results if a boolean parameter is true, is false or it is not present.

My method id something like

-(void) createRequestWithID:(NSString *)id translate(NSNumber *)translate onlyLastVersions:(NSNumber *)onlyLastVersions

My method is part of a framework which will be used and called by other developers. That means, I cannot control what they pass to parameters such as translate which is a boolean (I use NSNumber as wrapper).

And here comes the problem. I need to be able to identify if the method was called pasing translate as true, false or nil. Because for those cases, I will have the request as translate="true", translate="false" or translate will be not present in the request.

I need a way to differentiate between true, false and nil.

Testing on gbd to try to find a way to differenciate it I get:

(gdb) p translateFALSE
$5 = (NSNumber *) 0x0

(gdb) p translateNIL
$6 = (NSNumber *) 0x0

(gdb) po translateFALSE
Can't print the description of a NIL object.

(gdb) po translateNIL
Can't print the description of a NIL object.

(gdb) po [translateFALSE class]
Can't print the description of a NIL object.

(gdb) po [translateNIL class]
Can't print the description of a NIL object.

So I see no way to do it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

趴在窗边数星星i 2025-01-02 12:41:45
if( translate ) {
  if( [translate boolValue] ) {
    // translate is present and is set (YES)
  } else {
    // translate is present and is not set (NO)
  }
} else {
  // translate is nil / not present
}

如果您希望它看起来更像三向分支:

if( [translate boolValue] ) {
  // translate is present and is set (YES)
} else if( translate ) {
  // translate is present and is not set (NO)
} else {
  // translate is nil / not present
}
if( translate ) {
  if( [translate boolValue] ) {
    // translate is present and is set (YES)
  } else {
    // translate is present and is not set (NO)
  }
} else {
  // translate is nil / not present
}

If you prefer to have this look more like a three-way branch:

if( [translate boolValue] ) {
  // translate is present and is set (YES)
} else if( translate ) {
  // translate is present and is not set (NO)
} else {
  // translate is nil / not present
}
满地尘埃落定 2025-01-02 12:41:45

对于布尔值来说,不存在 NULL(或 nil)这样的东西 — NULL 是一个指针值。 NSNumber 可以为 nil,但代表 BOOL NO 的 NSNumber 不会为 nil。

There is no such thing as NULL (or nil) for a boolean — NULL is a pointer value. An NSNumber can be nil, but an NSNumber representing the BOOL NO will not be nil.

飘然心甜 2025-01-02 12:41:45

如果你传递一个 NSNumber,那就是一个对象。您可以轻松区分拥有该对象还是拥有 nil 值。 (在尝试提取对象值之前执行此操作。)

If you're passing an NSNumber, that's an object. You can easily differentiate between having the object or having a nil value. (Do this before you attempt to extract the object value.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文