Question

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
Was it helpful?

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.

OTHER TIPS

You should look at +[NSValue valueWithBytes:objCType:] not a (__bridge) cast. Bridge is for other things.

e.g.: [NSValue value:&coordinate withObjCType:@encode(CLLocationCoordinate2D)];

I guess it's possible to encode whole array too

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top