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

For me, this is a fairly complete text. I understand the parts of each part of the code, but I cannot describe the logical flow of how it hangs together and works in general, starting with interpreting the "^" symbol after the completionHandler: method.

Can I ask for some help here to rewrite this code in a less concise way, which is less efficient but more visually understandable? I downloaded this code, and I can say that in the context of the program this 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]; }]; 
+4
source share
3 answers

The ^ symbol indicates the beginning of a block. Basically, this code inside the block (from ^{ to } ) is not called until the captureStillImageAsynchronouslyFromConnection method is captureStillImageAsynchronouslyFromConnection . Once the method has completed capturing the image, it then executes the methods inside the block. Using blocks is relatively new in the world of Mac and iPhone, but they save you a lot of messy delegate methods and are usually incredibly useful. They may seem complicated at first, but you will soon learn to love them.

+7
source

You use the ^ operator to declare a block variable and indicate the start of a block literal. The body of the block itself inside {}, as shown in this example (as usual with C, indicates the end of the statement):

Learn more about "Block Programming Topics" in the iOS Developer Library.

+3
source

This signals the unit. While I am editing this answer, I will put a link to my article on blocks, which is definitely "for dummies" ... http://compileyouidontevenknowyou.blogspot.com/2010/07/blocks-in-objective-c.html

  • Basically, captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: ( captureStillImageEtc , for example) takes a block of code as a completion handler.

  • This code will be executed by the captureStillImageEtc method and will run code that is defined between curly braces ( {} ).

  • You will notice that the code acts on the two variables imageSampleBuffer and error . Block signatures determine that they should be passed when captureStillImageEtc wants to run the block (repeat, code in braces).

Note I could take this place, but blocks are very similar to pointers to anonymous (or named) functions for most purposes and tasks.

+1
source

All Articles