Expression in FOR command (for (int i=0; i < ([arr count]-1);i++){})

后端 未结 3 866
时光说笑
时光说笑 2021-01-02 23:08

I have a problem that I can not understand

NSArray *emptyArr = @[];
for (int i=0; i < ([emptyArr count]-1) ; i++) {
    NSLog(@\"Did run for1\");
}
         


        
3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-02 23:48

    This is because the return type of count is an unsigned int. When you substract 1 from 0, you do not get -1. Instead you underflow to the highest possible unsigned int. The reason it works in the second version is because you cast it (implicitly) to an int in which the value -1 is legal.

提交回复
热议问题