在方法调用中的插入符号

use*_*344 2 objective-c ios

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

[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];
    }
}];
Run Code Online (Sandbox Code Playgroud)

我不理解的部分如下: ^(MKLocalSearchResponse *response, NSError *error) {

那个插入符号 ^ 和之后发生的事情是我不知道的.

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

Max*_*Max 5

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

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

定义一个块:

 ^{
     NSLog(@"This is a block");
}
Run Code Online (Sandbox Code Playgroud)

将块分配给变量:

void (^simpleBlock)(void);

// or

simpleBlock = ^{
    NSLog(@"This is a block");
};
Run Code Online (Sandbox Code Playgroud)

打电话给一个街区:

simpleBlock();
Run Code Online (Sandbox Code Playgroud)

使用块作为消息的参数:

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

    XYZWebTask *task = ...

    [task beginTaskWithCallbackBlock:^{
        [self hideProgressIndicator];
    }];
}
Run Code Online (Sandbox Code Playgroud)

示例来自Apple文档