将 CLLocationCoordinates2D 存储在 NSMutableArray 中
经过一番搜索,我得到了以下解决方案: 参考。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是另一种方法,使用内置类型
NSValue
,它正是为此目的而创建的:要检索值,请使用以下代码:
Here’s another way, using the builtin type
NSValue
which is made for exactly this purpose:To retrieve the value use the following code:
从 iOS 6 开始,有 NSValueMapKitGeometryExtensions< /a> for
NSValue
:并检索值:
NSValueMapKitGeometryExtensions
需要 MapKit.frameworkCLLocationCooperative2DMake()
需要 CoreLocation.framework,所以需要这些导入:
Starting with iOS 6, there's the NSValueMapKitGeometryExtensions for
NSValue
:And to retrieve the value:
The
NSValueMapKitGeometryExtensions
require MapKit.frameworkCLLocationCoordinate2DMake()
requires CoreLocation.framework,so these imports are required:
没有泄漏,只是浪费了堆内存。
你可以只使用
There's no leak, just a waste of heap memory.
You could just use