3 Replies
      Latest reply on Jul 12, 2015 12:37 PM by SpaceMan
      SpaceMan Level 1 Level 1 (10 points)

        Can sombody tell me why the following code works?  I know that it does.  Specifically, look at the 'dict' variable.  It is used in the fast itteration for loop.  But wait, it's also used within the completionHandler block of code, but it doesn't have a __block specifier.  Why does this work?

         

            __block NSMutableArray *dumArray = [NSMutableArray arrayWithCapacity:3];

            for (NSDictionary *dict in self.stuffArray) {

              

                NSURL *url = [NSURL URLWithString:urlString];

                NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

                request.timeoutInterval = kTimeoutInterval;

                [NSURLConnection sendAsynchronousRequest:request

                                                   queue:[NSOperationQueue mainQueue]

                                       completionHandler:^(NSURLResponse *response,

                                                           NSData *data, NSError *connectionError) {

                                         

                                           if (data.length > 0 && connectionError == nil) {

                                             

                                               NSDictionary *message = [NSJSONSerialization JSONObjectWithData:data

                                                                                                       options:0

                                                                                                         error:NULL];

                                               NSDictionary *daDict = [message objectForKey:@"result"];

                                               NSString *theInfo = daDict objectForKey:@“stuff”]];

                                             

                                               NSMutableDictionary *replacementDict = [dict mutableCopy];

                                               [replacementDict setObject:theInfo forKey:@“TheInfo”];

                                               NSDictionary *theDict = [[NSDictionary alloc] initWithDictionary:replacementDict];

                                               [dumArray addObject:theDict];

                                           }

                                       }];

            }