Strong Like Bull
Strong Like Bull

Reputation: 11317

Variable scope in Objective-C blocks

I have the following piece of code in a block and want to find out if declaring local variables in something like enumerate is cool? I really do not want to deal with __block etc so wanted to see if there are any issues with the following code:

 [self.assets enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
     NSData *imageToUpload = UIImageJPEGRepresentation([(UIImageView *)obj image], 90);
     NSString *imageName = [NSString stringWithFormat:@"Image%d",idx];
     NSString *fileName = [NSString stringWithFormat:@"Image%d.jpeg",idx];
 }];

Upvotes: 0

Views: 165

Answers (1)

Cyrille
Cyrille

Reputation: 25144

These variables are useless as-is. They're not stored anywhere else, and are autoreleased at the end of the runloop.

Consider your block as a self-contained function: it does not write anything outside of its scope, and does not return anything.

Upvotes: 4

Related Questions