我试图理解我在互联网上找到的一些代码。我试图调整它,以便我可以在我自己的程序中使用它。在我的程序中,我将此作为单例的实例方法。我了解大部分这是在做什么,但没有得到“块”部分。块是干什么用的?在我的实现中,我应该传递什么作为参数来代替 NSSet Photos。我不明白这一点,因为我实际上希望从该位置的服务器“获取”照片。那我送什么?
+ (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]);
}
}];
}