将 CLLocationCoordinates2D 存储在 NSMutableArray 中

发布于 2024-08-27 07:52:49 字数 704 浏览 5 评论 0原文

经过一番搜索,我得到了以下解决方案: 参考

CLLocationCoordinate2D* new_coordinate = malloc(sizeof(CLLocationCoordinate2D));
new_coordinate->latitude = latitude;
new_coordinate->longitude = longitude;
[points addObject:[NSData dataWithBytes:(void *)new_coordinate
length:sizeof(CLLocationCoordinate2D)]];
free(new_coordinate);

并访问它:

CLLocationCoordinate2D* c = (CLLocationCoordinate2D*) [[points objectAtIndex:0] bytes];

但是,有人声称这里存在内存泄漏?谁能告诉我泄漏在哪里以及如何修复。此外,是否有更好的方法在 NSMutableArray 中存储 CLLocationCooperative2D 列表?请提供示例代码,因为我是 Objective C 新手。

After some searching, I got the following solution :
reference.

CLLocationCoordinate2D* new_coordinate = malloc(sizeof(CLLocationCoordinate2D));
new_coordinate->latitude = latitude;
new_coordinate->longitude = longitude;
[points addObject:[NSData dataWithBytes:(void *)new_coordinate
length:sizeof(CLLocationCoordinate2D)]];
free(new_coordinate);

And access it as:

CLLocationCoordinate2D* c = (CLLocationCoordinate2D*) [[points objectAtIndex:0] bytes];

However, someone claims that there is a memory leak here? Can anyone suggest me where is the leak and how to fix it. Further, is there a better way of storing a list of CLLocationCoordinate2D in NSMutableArray? Please give sample code since I am an Objective C newbie.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

_蜘蛛 2024-09-03 07:52:49

这是另一种方法,使用内置类型 NSValue ,它正是为此目的而创建的:

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

要检索值,请使用以下代码:

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

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];
烟火散人牵绊 2024-09-03 07:52:49

从 iOS 6 开始,有 NSValueMapKitGeometryExtensions< /a> for NSValue

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

并检索值:

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

NSValueMapKitGeometryExtensions 需要 MapKit.framework
CLLocationCooperative2DMake() 需要 CoreLocation.framework
所以需要这些导入:

#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>

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

NSMutableArray *points = [NSMutableArray array];
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,
so these imports are required:

#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
冷了相思 2024-09-03 07:52:49

没有泄漏,只是浪费了堆内存。

你可以只使用

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

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)]];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文