当前位置: 动力学知识库 > 问答 > 编程问答 >

xcode - Objective-C, Comparison of unsigned expression < 0 is always false, error?

问题描述:

I'm getting the following warning Comparison of unsigned expression < 0 is always false.

I'm not sure how to fix it ?

if(topWindowIndex < 0)

It's defined as...

NSUInteger topWindowIndex = [allWindows count] - 1;

网友答案:

If [allWindows count] - 1 can never be negative, the test (and the code it controls) is unnecessary. If it can be negative, you should declare topWindowIndex as a signed type (int or NSInteger).

A better solution, IMO, is to store the count directly and use that instead:

NSUInteger numWindows = [allWindows count];
...
if (numWindows < 1) ...
网友答案:

This is dangerous

NSUInteger topWindowIndex = [allWindows count] - 1;

if [allWindows count] is ever 0, you'll get a very large number (nonsense) as topWindowIndex.

网友答案:

Basic answer from above answers:

Use NSInteger instead of NSUInteger (unsigned).

分享给朋友:
您可能感兴趣的文章:
随机阅读: