错误:将'float [3]'发送到不兼容类型'float'的参数

时间:2013-08-02 12:53:38

标签: iphone objective-c arrays

我有一个typedef结构,如下所示:

typedef struct {
    float Position[3];
    float Color[4];
    float TexCoord[2];
} Vertex;

我想迭代Position,Color和TexCoord的数据:

+ (void)arrayConverter: (Vertex *) v
{

    // Turn typeDef struct into seperate arrays
    NSMutableArray *position   = [[NSMutableArray alloc] init];

    for (int p=0; p<(sizeof(v->Position)/sizeof(v)); p++) {
        [position addObject:[NSNumber numberWithFloat:v[p].Position]]; // Error: Sending 'float[3]' to parameter of incompatible type 'float'
    }
}

传入的数据:

Vertex Square_Vertices[] = {
    // Front
    {{0.5, -0.5, 1}, {0, 0, 0, 1}, {1, 0}},
    {{0.5, 0.5, 1}, {0, 0, 0, 1}, {1, 1}},
    {{-0.5, 0.5, 1}, {0, 0, 0, 1}, {0, 1}},
    {{-0.5, -0.5, 1}, {0, 0, 0, 1}, {0, 0}},
    // Back
    {{0.5, 0.5, -1}, {0, 0, 0, 1}, {0, 1}},
    {{-0.5, -0.5, -1}, {0, 0, 0, 1}, {1, 0}},
    {{0.5, -0.5, -1}, {0, 0, 0, 1}, {0, 0}},
    {{-0.5, 0.5, -1}, {0, 0, 0, 1}, {1, 1}},
    // Left
    {{-0.5, -0.5, 1}, {0, 0, 0, 1}, {1, 0}},
    {{-0.5, 0.5, 1}, {0, 0, 0, 1}, {1, 1}},
    {{-0.5, 0.5, -1}, {0, 0, 0, 1}, {0, 1}},
    {{-0.5, -0.5, -1}, {0, 0, 0, 1}, {0, 0}},
    // Right
    {{0.5, -0.5, -1}, {0, 0, 0, 1}, {1, 0}},
    {{0.5, 0.5, -1}, {0, 0, 0, 1}, {1, 1}},
    {{0.5, 0.5, 1}, {0, 0, 0, 1}, {0, 1}},
    {{0.5, -0.5, 1}, {0, 0, 0, 1}, {0, 0}},
    // Top
    {{0.5, 0.5, 1}, {0, 0, 0, 1}, {1, 0}},
    {{0.5, 0.5, -1}, {0, 0, 0, 1}, {1, 1}},
    {{-0.5, 0.5, -1}, {0, 0, 0, 1}, {0, 1}},
    {{-0.5, 0.5, 1}, {0, 0, 0, 1}, {0, 0}},
    // Bottom
    {{0.5, -0.5, -1}, {0, 0, 0, 1}, {1, 0}},
    {{0.5, -0.5, 1}, {0, 0, 0, 1}, {1, 1}},
    {{-0.5, -0.5, 1}, {0, 0, 0, 1}, {0, 1}},
    {{-0.5, -0.5, -1}, {0, 0, 0, 1}, {0, 0}}
};

如何迭代数据并添加到我的NSMutableArray而不会出现此错误?

1 个答案:

答案 0 :(得分:1)

如果要迭代单个顶点的v->Position的所有元素,它应该是

for (int p=0; p<(sizeof(v->Position)/sizeof(v->Position[0])); p++) {
    [position addObject:[NSNumber numberWithFloat:v->Position[p]]];
}

UPDATE:对于顶点数组,您的方法可能如下所示:

+ (void)arrayConverter: (Vertex *)vertices count:(NSUInteger)count
{
    NSMutableArray *position   = [[NSMutableArray alloc] init];
    Vertex *v = vertices;
    for (NSUInteger i = 0; i < count; i++) {
        for (int p=0; p<(sizeof(v->Position)/sizeof(v->Position[0])); p++) {
            [position addObject:[NSNumber numberWithFloat:v->Position[p]]];
        }
        v++;
    }
}

如果

Vertex Square_Vertices[] = { ... }

是一个顶点数组,你可以调用方法

[YourClass arrayConverter:Square_Vertices count:(sizeof(Square_Vertices)/sizeof(Square_Vertices[0]))];