gpt4 book ai didi

ios - FOR 命令中的表达式 (for (int i=0; i < ([arr count]-1);i++){})

转载 作者:可可西里 更新时间:2023-11-01 03:52:17 24 4
gpt4 key购买 nike

我有一个问题,我无法理解

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

[emptyArr count] - 1 是 -1 但我的应用程序仍然运行 NSLog 命令!

如果我使用一个 int 变量:

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

然后我的应用程序不运行 NSLog 命令。

谁能帮帮我!

最佳答案

这是因为 count 的返回类型是 unsigned int。当您从 0 中减去 1 时,您不会得到 -1。相反,您下溢到可能的最高 unsigned int。它在第二个版本中工作的原因是因为您将它(隐式)转换为 int,其中值 -1 是合法的。

关于ios - FOR 命令中的表达式 (for (int i=0; i < ([arr count]-1);i++){}),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17039316/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com