IOS如何判断i是否循环结束?
IOS how to check whether the i finish looping?
我有一个正常循环的 for
循环。但是我想检查循环是否已经完成循环,如果是我想执行一个动作如果没有我想执行另一个动作。
代码如下:
- (void)viewDidLoad {
[super viewDidLoad];
[loadingview setHidden:NO];
NSLog(@"Response recieved");
output = [[NSMutableArray alloc] init];
feeds = [[NSMutableArray alloc] init];
deepsightSig = [[NSArray alloc] init];
lastEl = [item_pass lastObject];
for (int i = 0; i < item_pass.count; i++) {
NSString *soapMessage = //soap message
url = [NSURL URLWithString:@"https://abc/SWS/hellworld.asmx"];
theRequest = [NSMutableURLRequest requestWithURL:url];
msgLength = [NSString stringWithFormat:@"%lu", (unsigned long)[soapMessage length]];
[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: @"https://www.hello.com/helloworld" forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
[connection start];
if (i == item_pass.count - 1) {
// this is the end of the loop
}
}
}
您可以在ViewController中添加BOOL实例变量,通过在循环结束时更新变量来检查循环是否结束。喜欢
if (i == item_pass.count - 1) {
loopFinished = YES; // Added Variable to check loop if finished or not
}
您不需要检查循环是否结束:
for (int i = 0; i < item_pass.count; i++) {
//...
}
//this is the end of the loop
或者我在你的问题中遗漏了什么
好吧,让我们一起来分解一下。首先我们仔细看看 for
循环:
for (int i = 0; i < 5; i++) {
}
括号内分为三部分。
int i = 0
:即使没有阅读任何文档或任何有关编程的书籍,这看起来也是循环变量的初始设置i
。
i < 5
:这看起来像是某种状态。循环变量 i
应小于 5。这可能意味着当循环变量大于或等于 5 时循环结束。
i++
:呃,这就奇怪了。但是当我们用等价的表达式替换它时,它会变得更加清晰。 i++
等价于 i = i + 1
。现在很明显,这是在每个循环 之后执行的语句,但在评估结束条件 (i < 5
) before 之前执行的语句。
好吧,让我们假设我们仍然不真正理解循环是什么以及循环是做什么的。为了更好地理解,我们可以在循环中添加一个断点,让调试器帮助我们理解它。或者我们可以添加一条日志语句:
for (int i = 0; i < 5; i++) {
NSLog(@"i: %d", i);
}
这会产生输出:
i: 0
i: 1
i: 2
i: 3
i: 4
它告诉我们可以从循环内访问循环变量。不要让我们将循环中的代码更改为仅在第一次和最后一次迭代期间记录:
for (int i = 0; i < 5; i++) {
if (i == 0) {
NSLog(@"This might be the first iteration: i = %d", i);
} else if (i == 5 - 1) {
NSLog(@"This might be the last iteration: i = %d", i);
}
}
输出如下所示:
This might be the first iteration: i = 0
This might be the last iteration: i = 4
我希望这能回答你的问题。
我有一个正常循环的 for
循环。但是我想检查循环是否已经完成循环,如果是我想执行一个动作如果没有我想执行另一个动作。
代码如下:
- (void)viewDidLoad {
[super viewDidLoad];
[loadingview setHidden:NO];
NSLog(@"Response recieved");
output = [[NSMutableArray alloc] init];
feeds = [[NSMutableArray alloc] init];
deepsightSig = [[NSArray alloc] init];
lastEl = [item_pass lastObject];
for (int i = 0; i < item_pass.count; i++) {
NSString *soapMessage = //soap message
url = [NSURL URLWithString:@"https://abc/SWS/hellworld.asmx"];
theRequest = [NSMutableURLRequest requestWithURL:url];
msgLength = [NSString stringWithFormat:@"%lu", (unsigned long)[soapMessage length]];
[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: @"https://www.hello.com/helloworld" forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
[connection start];
if (i == item_pass.count - 1) {
// this is the end of the loop
}
}
}
您可以在ViewController中添加BOOL实例变量,通过在循环结束时更新变量来检查循环是否结束。喜欢
if (i == item_pass.count - 1) {
loopFinished = YES; // Added Variable to check loop if finished or not
}
您不需要检查循环是否结束:
for (int i = 0; i < item_pass.count; i++) {
//...
}
//this is the end of the loop
或者我在你的问题中遗漏了什么
好吧,让我们一起来分解一下。首先我们仔细看看 for
循环:
for (int i = 0; i < 5; i++) {
}
括号内分为三部分。
int i = 0
:即使没有阅读任何文档或任何有关编程的书籍,这看起来也是循环变量的初始设置i
。i < 5
:这看起来像是某种状态。循环变量i
应小于 5。这可能意味着当循环变量大于或等于 5 时循环结束。i++
:呃,这就奇怪了。但是当我们用等价的表达式替换它时,它会变得更加清晰。i++
等价于i = i + 1
。现在很明显,这是在每个循环 之后执行的语句,但在评估结束条件 (i < 5
) before 之前执行的语句。
好吧,让我们假设我们仍然不真正理解循环是什么以及循环是做什么的。为了更好地理解,我们可以在循环中添加一个断点,让调试器帮助我们理解它。或者我们可以添加一条日志语句:
for (int i = 0; i < 5; i++) {
NSLog(@"i: %d", i);
}
这会产生输出:
i: 0
i: 1
i: 2
i: 3
i: 4
它告诉我们可以从循环内访问循环变量。不要让我们将循环中的代码更改为仅在第一次和最后一次迭代期间记录:
for (int i = 0; i < 5; i++) {
if (i == 0) {
NSLog(@"This might be the first iteration: i = %d", i);
} else if (i == 5 - 1) {
NSLog(@"This might be the last iteration: i = %d", i);
}
}
输出如下所示:
This might be the first iteration: i = 0
This might be the last iteration: i = 4
我希望这能回答你的问题。