Ama*_*rsh 15 cocoa nsmutablearray nsdata
经过一番搜索,我得到了以下解决方案: 参考.
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);
Run Code Online (Sandbox Code Playgroud)
并将其访问为:
CLLocationCoordinate2D* c = (CLLocationCoordinate2D*) [[points objectAtIndex:0] bytes];
Run Code Online (Sandbox Code Playgroud)
但是,有人声称这里有内存泄漏?任何人都可以建议我在哪里泄漏以及如何解决它.此外,是否有更好的方法在NSMutableArray中存储CLLocationCoordinate2D列表?请提供示例代码,因为我是Objective C新手.
Nik*_*uhe 70
这是另一种方法,使用NSValue为此目的而构建的内置类型:
CLLocationCoordinate2D new_coordinate = { latitude, longitude };
[points addObject:[NSValue valueWithBytes:&new_coordinate objCType:@encode(CLLocationCoordinate2D)]];
Run Code Online (Sandbox Code Playgroud)
要检索该值,请使用以下代码:
CLLocationCoordinate2D old_coordinate;
[[points objectAtIndex:0] getValue:&old_coordinate];
Run Code Online (Sandbox Code Playgroud)
And*_*Ley 51
从iOS 6开始,NSValueMapKitGeometryExtensions用于NSValue:
NSMutableArray *points = [NSMutableArray array];
CLLocationCoordinate2D new_coordinate = CLLocationCoordinate2DMake(latitude, longitude);
[points addObject:[NSValue valueWithMKCoordinate:new_coordinate]];
Run Code Online (Sandbox Code Playgroud)
并检索值:
CLLocationCoordinate2D coordinate = [[points objectAtIndex:0] MKCoordinateValue];
Run Code Online (Sandbox Code Playgroud)
该NSValueMapKitGeometryExtensions要求MapKit.framework
CLLocationCoordinate2DMake()需要CoreLocation.framework
没有泄漏,只是浪费堆内存.
你可以使用
CLLocationCoordinate2D new_coordinate;
new_coordinate.latitude = latitude;
new_coordinate.longitude = longitude;
[points addObject:[NSData dataWithBytes:&new_coordinate length:sizeof(new_coordinate)]];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11524 次 |
| 最近记录: |