19

Possible Duplicate:
Caret in objective C
What does this ^ syntax mean in Objective-C?

I am tired by searching the meaning of symbol ^ in Objective C. I have seen it in lot of projects especially in back ground running tasks. I will put a link http://developer.apple.com/library/ios/#samplecode/StitchedStreamPlayer/Introduction/Intro.html#//apple_ref/doc/uid/DTS40010092 and in MyStreamingMovieViewController.m you can find the following inside - (IBAction)endScrubbing:(id)sender method.

timeObserver = [[player addPeriodicTimeObserverForInterval:CMTimeMakeWithSeconds(tolerance, NSEC_PER_SEC) queue:dispatch_get_main_queue() usingBlock:
                         ^(CMTime time)
                         {
                             [self syncScrubber];
                         }] retain];
    }

Also http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/ManagingYourApplicationsFlow/ManagingYourApplicationsFlow.html

- (void)applicationDidEnterBackground:(UIApplication *)application
{
UIApplication*    app = [UIApplication sharedApplication];

bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
    // Clean up any unfinished task business by marking where you.
    // stopped or ending the task outright.
    [app endBackgroundTask:bgTask];
    bgTask = UIBackgroundTaskInvalid;
}];

// Start the long-running task and return immediately.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

    // Do the work associated with the task, preferably in chunks.

    [app endBackgroundTask:bgTask];
    bgTask = UIBackgroundTaskInvalid;
});
}

Please let me know the answer.

Community
  • 1
  • 1
rakeshNS
  • 4,227
  • 4
  • 28
  • 42

3 Answers3

22

That symbol is used to declare block.

For more information read here Blocks Programming Topics

Some more info:

Block objects are a C-level syntactic and runtime feature. They are similar to standard C functions, but in addition to executable code they may also contain variable bindings to automatic (stack) or managed (heap) memory. A block can therefore maintain a set of state (data) that it can use to impact behavior when executed.

You can use blocks to compose function expressions that can be passed to API, optionally stored, and used by multiple threads. Blocks are particular useful as a callback because the block carries both the code to be executed on callback and the data needed during that execution.

Nekto
  • 17,837
  • 1
  • 55
  • 65
  • Good link, but the answer itself does not contain any info. It would be great if you can explain what a `block` is. – Johan Nov 10 '11 at 09:49
  • Added some more info but it was copy-pasted from referenced document. – Nekto Nov 10 '11 at 09:52
  • 1
    @Johan: Given Nekto's answer, which was posted just before mine, and links to the first page of the same document, while the text I quoted that specifically answers your question is on the top of the second page, you're not trying very hard here to understand the answer to your question... – Duncan Babbage Nov 10 '11 at 09:54
  • @DuncanBabbage, it's not my question :-) – Johan Nov 10 '11 at 10:03
  • @Johan... whoops! My mistake. :) – Duncan Babbage Nov 10 '11 at 10:07
4

From the second page of Apple's Blocks Programming Topics:

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):

Duncan Babbage
  • 19,972
  • 4
  • 56
  • 93
4

That symbol is used to declare block.Blocks are addressable sections of code implemented inline (inside other functions). The inline-edness can be convenient but the real reason why blocks are different to regular functions and function pointers is that they can reference local variables from the scope of the function surrounding their implementation without the invoker of the block needing to know of the surrounding scope variables' existence.
How blocks are implemented (and the consequences)

Parag Bafna
  • 22,812
  • 8
  • 71
  • 144