Objective-C ^运算符

Hac*_*ckU 9 objective-c ios

我是Objective C的新手并试图找出^运算符是什么?在探索一些源代码时,我看到了下一个构造:

dispatch_once(&onceToken, ^{
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(20.f, 13.f), NO, 0.0f);

    [[UIColor blackColor] setFill];
    [[UIBezierPath bezierPathWithRect:CGRectMake(0, 0, 20, 1)] fill];
    [[UIBezierPath bezierPathWithRect:CGRectMake(0, 5, 20, 1)] fill];
    [[UIBezierPath bezierPathWithRect:CGRectMake(0, 10, 20, 1)] fill];

    [[UIColor whiteColor] setFill];
    [[UIBezierPath bezierPathWithRect:CGRectMake(0, 1, 20, 2)] fill];
    [[UIBezierPath bezierPathWithRect:CGRectMake(0, 6,  20, 2)] fill];
    [[UIBezierPath bezierPathWithRect:CGRectMake(0, 11, 20, 2)] fill];   

    defaultImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

});
Run Code Online (Sandbox Code Playgroud)

而且我想知道^是什么?

Pet*_*tar 17

^指示的目标C内的块定义的开始.

请看这里:http://developer.apple.com/library/ios/#documentation/cocoa/Conceptual/Blocks/Articles/bxGettingStarted.html#//apple_ref/doc/uid/TP40007502-CH7-SW1

请注意,在此上下文中,^它不是运算符,它是Objective-C语法的一部分@Mike的向下投票答案严格地说是"^运算符"的正确定义


iPa*_*tel 6

它在目标C中称为

句法:

returnType (^variableName)(parameters);
Run Code Online (Sandbox Code Playgroud)

Akiel Khan教程中逐字逐句,(你可以在这里找到另一个好的教程):

  • 块文字是"匿名的"(即无名)
  • 插入符号(^)符号
  • 我们没有必要指定返回类型 - 编译器可以"推断"它.如果我们愿意,我们可以明确地提到它.

这是官方文档,请阅读以获取更多信息.