目标C中符号^的含义

时间:2011-11-10 09:43:08

标签: iphone objective-c ios objective-c-blocks background-process

  

可能重复:
  Caret in objective C
  What does this ^ syntax mean in Objective-C?

我厌倦了在目标C中搜索符号^的含义。我在很多项目中都看到了它,特别是在后台运行任务中。我会放一个链接 http://developer.apple.com/library/ios/#samplecode/StitchedStreamPlayer/Introduction/Intro.html#//apple_ref/doc/uid/DTS40010092MyStreamingMovieViewController.m中,您可以在(IBAction)endScrubbing:(id)sender method内找到以下内容。

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

另外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;
});
}

请让我知道答案。

3 个答案:

答案 0 :(得分:19)

该符号用于声明阻止。

有关详细信息,请阅读Blocks Programming Topics

更多信息:

  

块对象是C级语法和运行时功能。他们是   类似于标准C函数,但除了可执行代码   它们还可能包含对自动(堆栈)或的自动变量绑定   托管(堆)内存。因此,块可以保持一组状态   (数据)它可以用来影响执行时的行为。

     

您可以使用块来组合可以传递的函数表达式   到API,可选地存储,并由多个线程使用。块是   特别适用于回调,因为块携带两者   要在回调上执行的代码以及在此期间所需的数据   执行。

答案 1 :(得分:4)

Apple's Blocks Programming Topics的第二页开始:

  

使用^运算符声明块变量并指示块文字的开头。块本身包含在{}中,如本例所示(与C一样,;表示语句的结尾):

答案 2 :(得分:4)

该符号用于声明block.Blocks是内联实现的代码的可寻址部分(在其他函数内部)。内联编辑可以很方便,但是块与常规函数和函数指针不同的真正原因是它们可以从围绕其实现的函数范围引用局部变量,而不需要块的调用者需要知道周围的范围变量的存在。
How blocks are implemented (and the consequences)