简体   繁体   English

将NSIntegers(或int)与NSUInteger(或unsigned int)进行比较

[英]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.

相关问题 比较数组中的整数:(无效的操作数为二进制表达式(“ NSUInteger *”(又名“ unsigned int *”)和“ NSUInteger *”) - Comparing integers in an array: (Invalid operands to binary expression ('NSUInteger *' (aka 'unsigned int *') and 'NSUInteger *') 另一个'NSUInteger *'(aka'unsigned int *')与'NSUInteger'(aka'unsigned int') - Another 'NSUInteger *' (aka 'unsigned int *') vs 'NSUInteger' (aka 'unsigned int') 错误的接收器类型“ NSUInteger *”(又名“ unsigned int *”) - Bad receiver type 'NSUInteger *' (aka 'unsigned int *') NSUInteger 与 NSInteger、int 与 unsigned 以及类似情况 - NSUInteger vs NSInteger, int vs unsigned, and similar cases 语义问题:指向整数转换的指针不兼容,将'NSUInteger *'(又名'unsigned int *')发送到'NSUInteger'类型的参数 - Semantic Issue: Incompatible pointer to integer conversion sending 'NSUInteger *' (aka 'unsigned int *') to parameter of type 'NSUInteger' int或NSInteger到NSUInteger获取索引 - int or NSInteger to NSUInteger for index 关于NSUinteger和int的问题 - Questions about NSUinteger and int int,NSInteger和NSUInteger之间的区别 - Difference between int, NSInteger and NSUInteger 隐式转换在制作应用程序时将整数精度'NSUInteger'(aka'unsigned long')转换为'int'吗? - implicit conversion loses integer precision 'NSUInteger' (aka 'unsigned long') to 'int' when making app? 隐式转换将整数精度'NSUInteger'(aka'unsigned long')丢失为'int'警告? - implicit conversion loses integer precision 'NSUInteger' (aka 'unsigned long') to 'int' warning?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM