LoginSignup
2
2

More than 5 years have passed since last update.

int nonNegative = MAX(0, [array count] - someInt)が負の値になってしまう

Posted at

はまったのでメモ

int nonNegative = MAX(0, [array count] - someInt)

みたいなのを書いて、値が負にならないことを保証しようとしたが、この書き方は誤り。

0が普通の int であるのに対して、 [array count]NSUInteger を返すらしい。
[array count] - someIntunsigned intなので負の数を表現出来ない。-1とかをunsigned intとして解釈すると、とても大きい数となってしまうので、結局上の式は想定外の動作をする。

ちなみに

if (0 < [array count] - someInt)

と書いた場合はちゃんとコンパイラが違う型同士の比較だという警告を出してくれる。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2