Objective C read-only int? What?

Mason picture Mason · Jul 18, 2011 · Viewed 7.5k times · Source

I made an int to count how many successes one of my processes has. Outside my code, I declare:

int successes = 0.

Then within my loop, I have successes++;, at which point XCode complains that "variable is not assignable (missing _block type specifier)".

What is going on? Why can't I increment my int? I never declared it read-only...

Any help is much appreciated.

The code I used is:

_block int successes = 0;

for(CLLocation *location in locationOutputArray)
{
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error)
     {
         if(placemarks && placemarks.count > 0)
         {
             successes++;
             CLPlacemark *topResult = [placemarks objectAtIndex:0];

             NSString *address = [NSString stringWithFormat:@"%@ %@,%@ %@", [topResult subThoroughfare],[topResult thoroughfare],[topResult locality], [topResult administrativeArea]];

             [addressOutputArray addObject:address];
             NSLog(@"%@",address);
         }

     }];

    [geocoder release];
}

Answer

John Calsbeek picture John Calsbeek · Jul 18, 2011

Your loop is inside a block (^{...} syntax). Blocks cannot alter variables outside of the block without that variable having a __block specifier.