Ios 块对象需要进一步解释

Ios 块对象需要进一步解释,ios,iphone,objective-c,anonymous-function,Ios,Iphone,Objective C,Anonymous Function,我了解块对象是如何工作的-但我没有遇到过这种类型的块对象,我不确定以下代码的作用: [localSearch startWithCompletionHandler:^(MKLocalSearchResponse *response, NSError *error) { if (!error) { int i = 0; do { MKMapItem *mapItem = [response.mapIt

我了解块对象是如何工作的-但我没有遇到过这种类型的块对象,我不确定以下代码的作用:

 [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];
        }
    }];
}
[localSearch startWithCompletionHandler:^(MKLocalSearchResponse*响应,NSError*错误){
如果(!错误){
int i=0;
做{
MKMapItem*mapItem=[response.mapItems objectAtIndex:i];
[self.mapItems addObject:mapItem];
i++;
}而(i

如果有人能解释一下这里发生了什么,我将不胜感激。

您需要记住,有两种不同的技术:块和通知。 块startWithCompletionHandler在搜索完成时,在处理来自搜索的数据并通过NSNotification发送之后被调用

您可以在应用程序中的任何位置订阅接收通知

阅读更多关于
ObjC中的块只是一个回调函数

在上面的函数localSearch.startWithCompletionHandler中,回调接受两个参数:response和error

调用此块时,搜索完成后,上述代码将在“response”中遍历所有键/值对,并将它们添加到名为self.mapItems的本地键/值映射中

之后,它会通过NSNotificationCenter发布一个带有“找到地址”的通知

若error不为NULL,即提供了指向NSError的指针,它将只发送一条错误通知消息


希望这有帮助。

插入符号(^)后的函数是一个“块”

localSearch对象将在需要时(完成后)调用下面定义的块(匿名函数)。这就像是一个委托,被指派处理操作的结果,但不需要您定义一个新函数来接收回调。调用类是委托(即self指函数中的调用方)

我添加了一些关于具体细节的评论

   (MKLocalSearchResponse *response, NSError *error) {
// If there is not an error
        if (!error) {
            int i = 0;
            do {
                MKMapItem *mapItem = [response.mapItems objectAtIndex:i];
// The map items found are added to a mapItems NSMutableArray (presumed) that is 
// owned by the original caller of this function.
                [self.mapItems addObject:mapItem];
                i++;
            } while (i < response.mapItems.count);
// Using the Observer design pattern here.  Some other object must register for the
// @"Address Found" message.  If any were found, it will be called.
            [[NSNotificationCenter defaultCenter] postNotificationName:@"Address Found" object:self];
        } else {
// Using the notification center to announce "not found".  I am not sure if this is correct,
// since this would be the response if there was an error, and not found is not necessarily
// the same as an error occurring.
            [[NSNotificationCenter defaultCenter] postNotificationName:@"Not Found" object:self];
        }
(MKLocalSearchResponse*响应,NSError*错误){
//如果没有错误
如果(!错误){
int i=0;
做{
MKMapItem*mapItem=[response.mapItems objectAtIndex:i];
//找到的映射项将添加到mapItems NSMutableArray(假定)中,该数组为
//由此函数的原始调用方拥有。
[self.mapItems addObject:mapItem];
i++;
}而(i
这有用吗


是否有您试图解决的特定问题?

此块将在方法完成后运行(
startWithCompletionHandler:

它检查是否没有错误,并将通过
do while
循环枚举
response.mapItems
数组。它获取每个
MKMapItem
对象,并将其添加到
self.mapItems
数组中。完成所有数组后,将发出通知(找到地址)将发布,以便每个订阅它的对象都将被通知。如果出现错误,将发布一个通知(未找到)。

如果您“了解块对象如何工作”,那么您在这里到底不了解什么?令人困惑的部分是插入符号在做什么:startWithCompletionHandler:^(MKLocalSearchResponse*response,NSError*error)阅读引用的帖子。它详细介绍了符号。一开始它也让我感到奇怪……我对代理之类的东西很满意。