Search code examples
objective-cstructcllocationbridge

How to use bridge for CLLocationCoordinate2D


AM trying to add CLLocationCoordinate2D[] to NSMutableArray and send it as parameter. But (__bridge id) is crashing the app. Struct to id conversion is the problem. Could anyone please let me know how to use this please.

CLLocationCoordinate2D coordinates[1000];

coordinates[index] --- all the coordinates added to it in loop.

NSMutableArray *coorArray = [NSMutableArray array];
[coorArray addObject:(__bridge id)(coordinates)]; crashes here

Solution

  • Use:

    NSMutableArray *coorArray = [NSMutableArray array];
    [coorArray addObject:[NSValue valueWithPointer:coordinates]];
    

    Then when you want to retrieve the array of struct:

    CLLocationCoordinate2D coordinates[] = [coorArray objectAtIndex:0].pointerValue;
    

    A C array is not an object, so it can't be bridged.