Question

I'm learning Block Syntax but something goes wrong.

The first code is below:

CLLocationCoordinate2D centerCoordinate = [Location sharedManager].coordinate;
MKCoordinateSpan span = MKCoordinateSpanMake(0.01, 0.01);
MKCoordinateRegion coordinateRegion = MKCoordinateRegionMake(centerCoordinate, span);
[self.mapView setRegion:coordinateRegion animated:YES];

The below code (my first try) worked:

MKCoordinateRegion (^aRegion)() = ^(void)
{
    CLLocationCoordinate2D centerCoordinate = [Location sharedManager].coordinate;
    MKCoordinateSpan span = MKCoordinateSpanMake(0.01, 0.01);
    return MKCoordinateRegionMake(centerCoordinate, span);
};
[self.mapView setRegion:aRegion() animated:YES];

But the below code (my second try) doesn't work:

[self.mapView setRegion:(MKCoordinateRegion^(void)){
    CLLocationCoordinate2D centerCoordinate = [Location sharedManager].coordinate;
    MKCoordinateSpan span = MKCoordinateSpanMake(0.01, 0.01);
    return MKCoordinateRegionMake(centerCoordinate, span);
}];

The error message is below:

1. Block pointer to non-function type is invalid.
2. Unexpected type name 'CLLocationCoordinate2D': expected expression

enter image description here

How can I work this code? Thank you for your kindness.

Était-ce utile?

La solution

In your case the method provides the body to the block. So the returned value(MKCoordinateRegionMake) will be returned to the caller of the block, which is not present.(setRegion: is not the caller here).

What you can do instead is you can do this:

MKCoordinateRegion (^aRegion)() = ^(void)
{
   CLLocationCoordinate2D centerCoordinate = [Location sharedManager].coordinate;
   MKCoordinateSpan span = MKCoordinateSpanMake(0.01, 0.01);
   return MKCoordinateRegionMake(centerCoordinate, span);
};
   [self.mapView setRegion:aRegion() animated:YES];

Moreover if you still want a block-style execution you can try this:

[self.mapView setRegion:({
    CLLocationCoordinate2D centerCoordinate = [Location sharedManager].coordinate;
    MKCoordinateSpan span = MKCoordinateSpanMake(0.01, 0.01);
    MKCoordinateRegion region= MKCoordinateRegionMake(centerCoordinate, span);
    region;
})];

The second one is not a block but its a syntax.

Autres conseils

Let's make an answer out of these good comments:

It looks to me like the code that works actually executes the block and the block's return result is the parameter passed to setRegion: where the second attempt passes the block itself as a parameter to setRegion:, which is of course expecting an MKCoordinateRegion as its parameter, not a block.

And (credit to MartinR), you can force evaluation of the block with an extra set of parentheses:

EDIT I'll get this right eventually

[self.mapView setRegion:^MKCoordinateRegion(void){
    CLLocationCoordinate2D centerCoordinate = [Location sharedManager].coordinate;
    MKCoordinateSpan span = MKCoordinateSpanMake(0.01, 0.01);
    return MKCoordinateRegionMake(centerCoordinate, span);
}()];
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top