NSNotFound、NSInteger、NSUInteger 和 NSRange.location
NSNotFound
定义为 NSIntegerMax
,它定义为 LONG_MAX
。
NSRange.location
定义为 NSUInteger
。
某些 Foundation 方法返回一个 NSRange
,其位置值为 NSNotFound
。
因此,人们经常进行这种比较:
if (aRange.location == NSNotFound)
但这不是有符号整数和无符号整数之间的比较应该生成警告吗?
NSNotFound
is defined as NSIntegerMax
, which is defined as LONG_MAX
.
NSRange.location
is defined as a NSUInteger
.
Some Foundation methods return an NSRange
whose location value is NSNotFound
.
So, one often does this comparison:
if (aRange.location == NSNotFound)
But isn't that a comparison between signed and unsigned integers that should generate a warning?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在将无符号(64 位)变量与有符号常量进行比较,该常量也适合变量的范围(不是负数)。所以不需要警告。
当与两侧的负常量或变量进行比较时,通常会出现警告(即,一部分为或可以为负,而另一部分仅为正或零)。
旁注:NSNotFound 在 32 位和 64 位平台上是不同的,因此不要在任何地方硬编码或存储该值。
You are comparing an unsigned (64 bit) variable with a signed constant which also fits into the range of the variable (it is not negative). So no need for a warning.
The warnings appear typically when comparing against negative constants or variables on both sides (i.e. one part is or can be negative while the other is only positive or zero).
A side note: NSNotFound is different on 32 bit vs 64 bit platforms, so don't hardcode or store that value anywhere.