Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of the ^ character in the Objective-C code? [duplicate]

For me, this is quite a mouthful of code. I understand the pieces of each code part but I cannot describe the logic flow of what how it hangs together and works as a whole, starting with the interpretation of the '^' character after the completionHandler: method.

May I ask for some help here to re-write this code in a less compressed form that is less efficient but more visually understandable? I downloaded this code and I can state that in the context of the program, it is working code.

Thanks.

[[self stillImageOutput] captureStillImageAsynchronouslyFromConnection:videoConnection 
completionHandler:^(CMSampleBufferRef imageSampleBuffer, NSError *error) { 

CFDictionaryRef exifAttachments = CMGetAttachment(imageSampleBuffer, 
                                   kCGImagePropertyExifDictionary, NULL);

if (exifAttachments) {
   NSLog(@"attachements: %@", exifAttachments);
} 
else { 
   NSLog(@"no attachments");
}

NSData *imageData = [AVCaptureStillImageOutput 
                     jpegStillImageNSDataRepresentation:imageSampleBuffer];    

UIImage *image = [[UIImage alloc] initWithData:imageData];
[self setStillImage:image];

[image release];
[[NSNotificationCenter defaultCenter] 
  postNotificationName:kImageCapturedSuccessfully object:nil];

}];
like image 214
Ric Avatar asked Dec 22 '22 12:12

Ric


1 Answers

The ^ symbols the start of a block. Basically what it's doing is the code inside the block (from ^{ to }) isn't called until the method captureStillImageAsynchronouslyFromConnection is completed. Once the method has finished capturing the image, it then performs the methods inside the block. Using blocks is relatively new in the Mac & iPhone world, but they save you from lots of messy delegate methods and are generally awesome to use. At first they may seem daunting, but you'll learn to love them soon enough.

like image 175
sudo rm -rf Avatar answered May 14 '23 03:05

sudo rm -rf