![](/img/trans.png)
[英]How can I check whether or not Motion & Fitness is enabled on an iOS device?
[英]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
}
}
}
好吧,讓我們一起分解一下。 首先,我們仔細看一下for
循環:
for (int i = 0; i < 5; i++) {
}
括號內分為三部分。
int i = 0
:即使沒有閱讀文檔或任何有關編程的書,這看起來也像是循環變量i
的初始設置。 i < 5
:這看起來像是某種情況。 循環變量i
應該小於5。這可能意味着當循環變量變得大於或等於5時,循環結束。 i++
:嗯,這很奇怪。 但是,當我們用一個等效的表達式替換它時,它將變得更加清晰。 i++
等於i = i + 1
。 現在很明顯,這是在每個循環之后但在評估結束條件( i < 5
) 之前執行的語句。 好的,讓我們假設我們仍然不太真正了解循環是什么,循環是做什么的。 為了更好地理解,我們可以在循環中添加一個斷點,然后讓調試器幫助我們理解它。 或者我們可以添加一條日志語句:
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
我希望這回答了你的問題。
您可以在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
或者我在您的問題中遺漏了一些東西
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.