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

后端 未结 3 884
时光说笑
时光说笑 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

    The value returned by [emptyArr count] is unsigned integer. In the first case, [emptyArr count]-1 is 0-1 represented in 2's compliment, which is a huge number. So it prints the log many times.

    In the second case, [emptyArr count]-1 -> You are casting the result of this to int. 0-1 -> -1 signed int. Hence does not print.

提交回复
热议问题