简体   繁体   中英

BREAK statement not in loop or switch in Objective C

I have a set of array which I need to loop and identify each the url domain is passed 200 status code. IF the first index value is pass then I shall break the loop and save the url in local. here is my code:

for (int j = 0; j < items.count; j++){
                                NSString *urlStr2 = items[j];
                                [session GET:urlStr2 parameters:nil progress:^(NSProgress * _Nonnull uploadProgress) {
                                    // nil
                                } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                                    // The domain name request is successful, processing data
                                    
                                    NSURL *url = [NSURL URLWithString:urlStr2];
                                    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
                                    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
                                    [NSURLConnection cancelPreviousPerformRequestsWithTarget:self];
                                    
                                    [NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
                                        
                                        NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
                                        
                                        if ([httpResponse statusCode] == 200){
                                            
                                            h5UrlStr = urlStr2;
                                            break;
                                        }
                                    }];
                                    
                                } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                                    // Domain name request failed
                                    
                                    if (error.code == -1003) {
                                    }
                                }];
                            }

But the problem is when I place the break when the condition is meet, it will appear error saying that break statement not in loop or switch Objective C. How should I break the loop?

In this case break will not help you because you are calling it in lambda (anonymous function). It is completely different scope. As I can see in your code you are launching up to items.count parallel requests but you need results of only the first successful.

One possible solution is to assign names to callback functions, get rid of loop and make the same GET call with advanced element counter in case of each failure while j < items.count . In such case all request will be sequential, one after another until some of them succeed. It can take a time.

Another solution is to launch requests in parallel (probably not for all elements of items at once, but for some chunk) and on each successful call check if result was already obtained. If it was, simply ignore current and return. This is faster but wastes bandwidth and computing resources.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM