gpt4 book ai didi

cocoa - 将 CLLocationCoordinates2D 存储在 NSMutableArray 中

转载 作者:行者123 更新时间:2023-12-03 16:02:28 24 4
gpt4 key购买 nike

经过一番搜索,我得到了以下解决方案: 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);

并将其访问为:

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

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

最佳答案

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

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

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

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

关于cocoa - 将 CLLocationCoordinates2D 存储在 NSMutableArray 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2577428/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com