Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/100.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone iOS-AFNetworking-理解这个代码片段-它在做什么?_Iphone_Ios_Ios5_Ios6_Afnetworking - Fatal编程技术网

Iphone iOS-AFNetworking-理解这个代码片段-它在做什么?

Iphone iOS-AFNetworking-理解这个代码片段-它在做什么?,iphone,ios,ios5,ios6,afnetworking,Iphone,Ios,Ios5,Ios6,Afnetworking,我正在努力理解我在网上找到的一些代码。我试图调整它,这样我就可以在我自己的程序中使用它。在我的程序中,我把它作为一个单例的实例方法。我知道这是在做什么,但我不明白“块”的部分。这个街区是干什么的?在我的实现中,我应该传递什么作为参数来代替NSSet照片。我不明白这一点,因为我实际上希望从服务器上“获取”该位置的照片。那我要送什么 + (void)photosNearLocation:(CLLocation *)location block:(void (^)(

我正在努力理解我在网上找到的一些代码。我试图调整它,这样我就可以在我自己的程序中使用它。在我的程序中,我把它作为一个单例的实例方法。我知道这是在做什么,但我不明白“块”的部分。这个街区是干什么的?在我的实现中,我应该传递什么作为参数来代替NSSet照片。我不明白这一点,因为我实际上希望从服务器上“获取”该位置的照片。那我要送什么

 + (void)photosNearLocation:(CLLocation *)location
                 block:(void (^)(NSSet *photos, NSError *error))block
 {
    NSLog(@"photosNearLocation - Photo.m");
    NSMutableDictionary *mutableParameters = [NSMutableDictionary dictionary];
    [mutableParameters setObject:[NSNumber 
    numberWithDouble:location.coordinate.latitude] forKey:@"lat"];
    [mutableParameters setObject:[NSNumber 
    numberWithDouble:location.coordinate.longitude] forKey:@"lng"];

    [[GeoPhotoAPIClient sharedClient] getPath:@"/photos"
                               parameters:mutableParameters
                                  success:^(AFHTTPRequestOperation *operation, id JSON)
    {
      NSMutableSet *mutablePhotos = [NSMutableSet set];
      NSLog(@" Json value received is : %@ ",[JSON description]);
      for (NSDictionary *attributes in [JSON valueForKeyPath:@"photos"])
      {
        Photo *photo = [[Photo alloc]
                        initWithAttributes:attributes];
        [mutablePhotos addObject:photo];
      }

      if (block) {
        block([NSSet setWithSet:mutablePhotos], nil);
       }
     }failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
       if (block)
      {
        block(nil, error);
        NSLog(@"Error in Photo.m line 145 %@ ", [error description]);
       }
      }];
      }

不需要通过任何东西的照片集。这是块的一个参数。调用方的任务是传递一个块,该块将在方法完成一些异步工作时被调用。所以你可以这样称呼它:

// let's setup something in the caller's context to display the result
// and to demonstrate how the block is a closure - it remembers the variables
// in it's scope, even after the calling function is popped from the stack.

UIImageView *myImageView = /* an image view in the current calling context */;

[mySingleton photosNearLocation:^(NSSet *photos, NSError *error) {
    // the photo's near location will complete some time later
    // it will cause this block to get invoked, presumably passing
    // a set of photos, or nil and an error
    // the great thing about the block is that it can refer to the caller context
    // as follows....

    if (photos && [photos count]) {
        myImageView.image = [photos anyObject];   // yay.  it worked
    } else {
        NSLog(@"there was an error: %@", error);
    }
}];

谢谢你的回复。我还有一个问题,在原始函数中是否已经有代码来处理返回调用的成功和失败?如果这是正确的,那么我是否需要在调用者的方法中的函数之外添加您示例中的代码来处理该块?这让我很困惑。谁来处理块,调用方还是单例?看起来,一旦成功,方法就会将结果传递给块。失败时,它记录错误并使用nil和错误调用块。如果您只需要这样做,那么您只需传递一个空块,就像一个空方法(NSSet*set,NSError*error)^{}。但是,您之所以调用它,可能是因为您希望对这些图像执行一些操作,例如保存它们或将它们(或错误)呈现给用户。这就是你放在街区里的东西。