开发者

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

开发者 https://www.devze.com 2023-03-11 05:07 出处:网络
This question already has answers here: Caret character between types rather than variables, surrounded by parentheses
This question already has answers here: Caret character between types rather than variables, surrounded by parentheses (2 answers) Closed 8 years ago.

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];

}];


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.


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

You can read more at the Blocks Programming Topics at the iOS developer library.


That signals a block. While I'm editing this answer I'll 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 the captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler: (captureStillImageEtc, let's say) accepts a code block as the completion handler.

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

  • You'll note that the code acts upon the two variables imageSampleBuffer and error. The block signature defines that these will need to be passed in when captureStillImageEtc wants to run the block (to repeat, the code in the curly braces).

Note: I might get downvoted for this, but blocks are a lot like pointers to anonymous (or named) functions for most intents and purposes.

0

精彩评论

暂无评论...
验证码 换一张
取 消