在方法调用中插入符号 [英] Caret in method call

查看:89
本文介绍了在方法调用中插入符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读这个教程并遇到了这行代码让我难过:

I was going through this tutorial and came across this line of code which has stumped me:

[localSearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) {
    if (!error) {
        int i = 0;
        do {
            MKMapItem *mapItem = [response.mapItems objectAtIndex:i];
            [self.mapItems addObject:mapItem];
            i++;
        } while (i < response.mapItems.count);
        [[NSNotificationCenter defaultCenter] postNotificationName:@"Address Found" object:self];
    } else {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"Not Found" object:self];
    }
}];

我不理解的部分如下: ^(MKLocalSearchResponse *响应,NSError *错误){

The part which I don't understand is the following: ^(MKLocalSearchResponse *response, NSError *error) {

该插入符号 ^ 以及什么是之后发生的是我不知道的事情。

That caret symbol ^ and what is happening after that is what I don't know.

我查看了一些文档但找不到任何内容。

I looked through some documentation but could not find anything.

推荐答案

^ 表示目标c中的。它就像一个匿名函数,可以分配给变量或作为参数传递给函数,如示例所示。阅读Apple文档中的更多内容:

^ denotes a block in objective c. It is like an anonymous function that can be assigned to a variable or being passed as an argument to a function as seen in your example. Read more in the Apple docs:

https://developer.apple.com/library/ios/documentation/cocoa/Conceptual/Blocks/Articles/00_Introduction.html

定义一个区块:

 ^{
     NSLog(@"This is a block");
}

为变量分配块

void (^simpleBlock)(void);

// or

simpleBlock = ^{
    NSLog(@"This is a block");
};

拨打电话:

simpleBlock();

使用块作为消息的参数:

- (IBAction)fetchRemoteInformation:(id)sender {
    [self showProgressIndicator];

    XYZWebTask *task = ...

    [task beginTaskWithCallbackBlock:^{
        [self hideProgressIndicator];
    }];
}

示例取自Apple文档

Examples are taken from the Apple documentation

这篇关于在方法调用中插入符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆