[英]Comparing NSIntegers (or int) to NSUInteger (or unsigned int)
I have a fairly large app (objective-c), I decided to turn on the "Sign Comparison" warnings. 我有一个相当大的应用程序(objective-c),我决定打开“Sign Comparison”警告。 I now have almost 100 of them.
我现在差不多有100个。 Most of them are similar to this:
他们中的大多数都与此类似:
if (recentSearches.count > indexPath.row) {
//something
}
and the errors are similar to this: 并且错误与此类似:
Comparison of integers of different signs: 'NSUInteger' (aka 'unsigned int') and 'NSInteger' (aka 'int')
what are some good strategies or tips on how to tackle them all? 关于如何解决这些问题的一些好策略或技巧是什么? Thanks
谢谢
Really, you want to go ahead and change the declarations so the types match when you compare. 实际上,您希望继续更改声明,以便在比较时匹配类型。 For example, a
count
variable should be unsigned. 例如,
count
变量应该是无符号的。 That's by far the safest. 这是迄今为止最安全的。 If you are recycling a variable so it's compared to other things that may be both signed or unsigned, change that!
如果您正在回收变量,那么将其与可能已签名或未签名的其他内容进行比较,请更改它! Only typecast when you have explicitly verified it's safe do to so.
只有当您明确验证它是安全的时才进行类型转换。 For example, if a signed int is > 0 you can cast it to unsigned int.
例如,如果signed int> 0,则可以将其强制转换为unsigned int。 The other direction is not safe unless you handle the range of values an unsigned int can hold that are too big for a signed int!
另一个方向是不安全的,除非你处理unsigned int可以容纳的值范围,这对于signed int来说太大了!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.