如何在objective-c app中声明C样式数组?

时间:2022-10-01 22:24:58

I have two NSArrays, one of CLLocation and one of doubles (encased in objects) that I need to write to C vectors to draw a gradient MKPolyline as defined here (https://github.com/wdanxna/GradientPolyline). I tried to copy some of the code I saw there in preparation to call one of its functions:

我有两个NSArrays,一个是CLLocation,另一个是双打(包含在对象中),我需要写入C向量来绘制这里定义的渐变MKPolyline(https://github.com/wdanxna/GradientPolyline)。我试图复制我在那里看到的一些代码,准备调用它的一个函数:

points = malloc(sizeof(CLLocationCoordinate2D)*self.run.locations.array.count);
velocity = malloc(sizeof(float)*self.run.locations.array.count);

for(int i = 0; i<self.run.locations.array.count; i++){
    points[i] = self.run.locations.array[i];
    velocity[i] = [velocities[i] floatValue];
}

Here self.run.locations.array is an array of CLLocations.

这里的self.run.locations.array是一个CLLocations数组。

Right now I can't even build the project because I have not declared the variables. But where/how do I declare these variables? I don't know C, and the project I am trying to use doesn't seem to include these declarations in a place I can find them.

现在我甚至无法构建项目,因为我没有声明变量。但是我在哪里/如何声明这些变量?我不知道C,我试图使用的项目似乎没有在我能找到的地方包含这些声明。

1 个解决方案

#1


points is an array of CLLocationCoordinate2D (dynamically allocated) so it should be a pointer to CLLocationCoordinate2D i.e.

points是CLLocationCoordinate2D(动态分配)的数组,因此它应该是指向CLLocationCoordinate2D的指针,即

CLLocationCoordinate2D *points;

velocity is an array of float so it should be declared as

velocity是一个浮点数组,因此应该声明为

float *velocity;

Alternatively you can do this

或者你可以这样做

float velocity[];

if you prefer array syntax.

如果您更喜欢数组语法。

#1


points is an array of CLLocationCoordinate2D (dynamically allocated) so it should be a pointer to CLLocationCoordinate2D i.e.

points是CLLocationCoordinate2D(动态分配)的数组,因此它应该是指向CLLocationCoordinate2D的指针,即

CLLocationCoordinate2D *points;

velocity is an array of float so it should be declared as

velocity是一个浮点数组,因此应该声明为

float *velocity;

Alternatively you can do this

或者你可以这样做

float velocity[];

if you prefer array syntax.

如果您更喜欢数组语法。