问题描述:

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).

相关阅读:
Top