使用CLLocationCoordinate2D填充malloc-ed数组 [英] Populating malloc-ed array with CLLocationCoordinate2D
问题描述
到目前为止,我对Objective-C和Cocoa框架留下了深刻的印象,但我不得不说,我有点困惑的最好的方式来创建一个CLLocationCoordinate2D数组传递给polylineWithCoordinates:count:I pretty pretty确保这是我需要设置数组。
So far I have been very impressed with Objective-C and the Cocoa Frameworks but I have to say that I am a little confused by the best way to create an array of CLLocationCoordinate2D to pass to the polylineWithCoordinates:count: I am pretty sure that this is how I need to setup the array.
NSUInteger numPoints = [locationData count];
CLLocationCoordinate2D *arrayPtr = malloc(numPoints * sizeof(CLLocationCoordinate2D));
但我不知道如何访问每个位置,我想我可以使用arrayPtr [i]
But I am not sure how to access each location, I thought I could use arrayPtr[i] but I can't seem to get that to work properly.
for(NSUInteger counter=0; counter<numPoints; counter++) {
arrayPtr[counter] = [[locationData objectAtIndex:counter] coordinate];
}
EDIT:
我现在工作,现在,我正在查看内存错误在调试器,但有另一种方式(也许没有malloc)我尝试编码的值在NSArray使用NSValue,只是发现我需要一个c型数组
I have it working now, I was viewing the memory wrong in the debugger, but is there another way (maybe without malloc) I did try encoding the values in an NSArray using NSValue, only to find that I need a c-type array instead :( any alternative methods would be most welcome.
推荐答案
这应该是有效的 - 你得到什么错误?
That should be valid - what error are you getting?
一个更好的方法可能是使用CFArray而不是手动管理malloc。
A better approach might be to use CFArray instead of manually managing the malloc.
这篇关于使用CLLocationCoordinate2D填充malloc-ed数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!