Storing CLLocationCoordinates2D in NSMutableArray

强颜欢笑 提交于 2019-11-28 18:52:11

There's no leak, just a waste of heap memory.

You could just use

CLLocationCoordinate2D new_coordinate;
new_coordinate.latitude = latitude;
new_coordinate.longitude = longitude;
[points addObject:[NSData dataWithBytes:&new_coordinate length:sizeof(new_coordinate)]];
Nikolai Ruhe

Here’s another way, using the builtin type NSValue which is made for exactly this purpose:

CLLocationCoordinate2D new_coordinate = { latitude, longitude };
[points addObject:[NSValue valueWithBytes:&new_coordinate objCType:@encode(CLLocationCoordinate2D)]];

To retrieve the value use the following code:

CLLocationCoordinate2D old_coordinate;
[[points objectAtIndex:0] getValue:&old_coordinate];

Starting with iOS 6, there's the NSValueMapKitGeometryExtensions for NSValue:

CLLocationCoordinate2D new_coordinate = CLLocationCoordinate2DMake(latitude, longitude);
[points addObject:[NSValue valueWithMKCoordinate:new_coordinate]];

And to retrieve the value:

CLLocationCoordinate2D coordinate = [[points objectAtIndex:0] MKCoordinateValue];

The NSValueMapKitGeometryExtensions require MapKit.framework
CLLocationCoordinate2DMake() requires CoreLocation.framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!