gpt4 book ai didi

ios - for 语句循环未按预期工作

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:45:53 24 4
gpt4 key购买 nike

我不明白为什么这个for循环不会循环!任何帮助表示赞赏。出于某种原因,i++ 无法正常工作??

busTypeForSectionsFirst 包含多个字母的数组,即从 A 到 Z。(每个字母的数量不同)

tempArray 是一个只有 A 到 Z 的数组。

我的日志看起来像这样:x26 次。

2013-01-08 11:17:53.596 App[969:c07] i = 0

2013-01-08 11:17:53.596 App[969:c07] Count of i in array = 2

2013-01-08 11:17:53.596 App[969:c07] Object searched - A

NSCountedSet *countedSet = [[NSCountedSet alloc] initWithArray:busTypeForSectionsFirst];

NSLog(@"%@", countedSet);

for (int i=0; i<=26; i++) {
if (![countedSet countForObject:[tempArray objectAtIndex:i]]) {
NSLog(@"Nil");
NSLog(@"i = %i", i);
NSLog(@"Count of i in array = %i", [countedSet countForObject:[tempArray objectAtIndex:i]]);
NSLog(@"Object searched - %@", [tempArray objectAtIndex:i]);
return 0;
} else {
NSLog(@"i = %i", i);
NSLog(@"Count of i in array = %i", [countedSet countForObject:[tempArray objectAtIndex:i]]);
NSLog(@"Object searched - %@", [tempArray objectAtIndex:i]);
return 0;
return [countedSet countForObject:[tempArray objectAtIndex:i]];
}}

return 0;

最佳答案

不确定您要实现的目标。但你或许可以试试这段代码,

NSCountedSet *countedSet = [[NSCountedSet alloc] initWithArray:busTypeForSectionsFirst];

NSLog(@"%@", countedSet);

NSInteger returnVal = 0;

for (int i=0; i<=26; i++) {
if (![countedSet countForObject:[tempArray objectAtIndex:i]]) {
NSLog(@"Nil");
NSLog(@"i = %i", i);
NSLog(@"Count of i in array = %i", [countedSet countForObject:[tempArray objectAtIndex:i]]);
NSLog(@"Object searched - %@", [tempArray objectAtIndex:i]);
} else {
NSLog(@"i = %i", i);
NSLog(@"Count of i in array = %i", [countedSet countForObject:[tempArray objectAtIndex:i]]);
NSLog(@"Object searched - %@", [tempArray objectAtIndex:i]);
returnVal = [countedSet countForObject:[tempArray objectAtIndex:i]];
//break; //probably a break statement here is what you are looking for
}
}

return returnVal;

如果您在循环中放置一个 return 语句,它将退出并从该方法返回。在您的情况下,您试图同时返回 ifelse 条件,这将导致 for 循环仅执行一次。并且您的 return [countedSet countForObject:[tempArray objectAtIndex:i]]; 将永远不会执行,因为您在此之前添加了 return 0;

如果你想中断一个for循环,你需要使用break;语句。 return 将从该方法返回并且不会执行下面的其余代码。

关于ios - for 语句循环未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14206907/

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