Iphone 将typdef结构转换为NSMutableArray

Iphone 将typdef结构转换为NSMutableArray,iphone,ios,objective-c,opengl-es,Iphone,Ios,Objective C,Opengl Es,我试图找出如何获取typdef结构,如以下所示: typedef struct { float Position[3]; float Color[4]; float TexCoord[2]; } const Vertex; 并将其转换为单独的阵列 我将以下内容作为转换数组: + (...)arrayConverter: (Vertex *) v { // Turn typeDef struct into seperate arrays NSMutable

我试图找出如何获取typdef结构,如以下所示:

typedef struct {
    float Position[3];
    float Color[4];
    float TexCoord[2];
} const Vertex;
并将其转换为单独的阵列

我将以下内容作为转换数组:

+ (...)arrayConverter: (Vertex *) v
{
    // Turn typeDef struct into seperate arrays
    NSMutableArray *position          = [[NSMutableArray alloc] init];
    NSMutableArray *color             = [[NSMutableArray alloc] init];
    NSMutableArray *texcord           = [[NSMutableArray alloc] init];
    const NSMutableArray *vertexdata  = [[NSMutableArray alloc] init];

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

    for (int c=0; c<(sizeof(v->Color)/sizeof(v)); c++) {
        [color addObject:[NSNumber numberWithFloat:v->Color[c]]];
    }

    for (int t=0; t<(sizeof(v->TexCoord)/sizeof(v)); t++) {
        [texcord addObject:[NSNumber numberWithFloat:v->TexCoord[t]]];
    }

    [vertexdata addObjectsFromArray:position];
    [vertexdata addObjectsFromArray:color];
    [vertexdata addObjectsFromArray:texcord];

NSLog(@"\n sizeof Position: %lu\n sizeof Color: %lu\n sizeof TexCoord: %lu\n sizeof Vertex: %lu\n", sizeof(v->Position), sizeof(v->Color), sizeof(v->TexCoord), sizeof(v));
NSLog(@"\n Position array: %@\n Color array: %@\n TexCord array: %@\n",position, color, texcord);
NSLog(@"\n Vertex data: %@\n",vertexdata);

    return vertexdata;
}
NSLog信息:

2013-07-31 01:42:45.346
 sizeof Position: 12
 sizeof Color: 16
 sizeof TexCoord: 8
 sizeof Vertex: 4
2013-07-31 01:42:45.347 
 Position array: (
    "0.5",
    "-0.5",
    1
)
 Color array: (
    0,
    0,
    0,
    1
)
 TexCord array: (
    1,
    0
)
2013-07-31 01:42:45.348  
 Vertex data: (
    "0.5",
    "-0.5",
    1,
    0,
    0,
    0,
    1,
    1,
    0
)
编辑(根据您的问题):

 NSMutableArray *array = [NSMutableArray array];
 Vertex data;
 int i;
  for (i = 1;  i <= yourCount;  i++) 
  {
     NSValue *value = [NSValue valueWithBytes:&data objCType:@encode(Vertex)];
     [array addObject:value];
  }
例如:

float postionValue = myNode->Position[0];
float colorValue = myNode->Color[1];
float textCoordValue = myNode->TexCoord[1];
编辑(根据您的问题):

 NSMutableArray *array = [NSMutableArray array];
 Vertex data;
 int i;
  for (i = 1;  i <= yourCount;  i++) 
  {
     NSValue *value = [NSValue valueWithBytes:&data objCType:@encode(Vertex)];
     [array addObject:value];
  }
例如:

float postionValue = myNode->Position[0];
float colorValue = myNode->Color[1];
float textCoordValue = myNode->TexCoord[1];

在添加到阵列之前,可以尝试将顶点结构转换为NSData。在NSArray中存储结构要简单得多

// make a NSData object
NSData *myData = [NSData dataWithBytes:&myStruct length:sizeof(myStruct)];

// make a new PacketJoin
MyStruct myStruct;
[myData getBytes:&myStruct length:sizeof(myStruct)];

在添加到阵列之前,可以尝试将顶点结构转换为NSData。在NSArray中存储结构要简单得多

// make a NSData object
NSData *myData = [NSData dataWithBytes:&myStruct length:sizeof(myStruct)];

// make a new PacketJoin
MyStruct myStruct;
[myData getBytes:&myStruct length:sizeof(myStruct)];

对不起,我误解了你的问题。 现在我明白了,您需要将1个垂直位置、颜色和纹理坐标输出到数组中。 这么快的答案是

+ (...)arrayConverter: (Vertex *) v
{
    // Turn typeDef struct into seperate arrays
    NSMutableArray *position          = [[NSMutableArray alloc] init];
    NSMutableArray *color             = [[NSMutableArray alloc] init];
    NSMutableArray *texcord           = [[NSMutableArray alloc] init];
    const NSMutableArray *vertexdata  = [[NSMutableArray alloc] init];

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

    for (int c=0; c<4; c++) {
        [color addObject:[NSNumber numberWithFloat:v->Color[c]]];
    }

    for (int t=0; t<2; t++) {
        [texcord addObject:[NSNumber numberWithFloat:v->TexCoord[t]]];
    }

    [vertexdata addObjectsFromArray:position];
    [vertexdata addObjectsFromArray:color];
    [vertexdata addObjectsFromArray:texcord];

    return vertexdata;
}
+(…)阵列转换器:(顶点*)v
{
//将typeDef结构转换为单独的数组
NSMUTABLEARRY*位置=[[NSMUTABLEARRY alloc]init];
NSMUTABLEARRY*color=[[NSMUTABLEARRY alloc]init];
NSMutableArray*texcord=[[NSMutableArray alloc]init];
常量NSMutableArray*vertexdata=[[NSMutableArray alloc]init];
for(int p=0;position[p]];
}
对于(int c=0;cColor[c]];
}
for(int t=0;tTexCoord[t]];
}
[vertexdata addObjectsFromArray:位置];
[vertexdata addObjectsFromArray:color];
[vertexdata addObjectsFromArray:texcord];
返回顶点数据;
}

对不起,我误解了你的问题。 现在我明白了,您需要将1个垂直位置、颜色和纹理坐标输出到数组中。 这么快的答案是

+ (...)arrayConverter: (Vertex *) v
{
    // Turn typeDef struct into seperate arrays
    NSMutableArray *position          = [[NSMutableArray alloc] init];
    NSMutableArray *color             = [[NSMutableArray alloc] init];
    NSMutableArray *texcord           = [[NSMutableArray alloc] init];
    const NSMutableArray *vertexdata  = [[NSMutableArray alloc] init];

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

    for (int c=0; c<4; c++) {
        [color addObject:[NSNumber numberWithFloat:v->Color[c]]];
    }

    for (int t=0; t<2; t++) {
        [texcord addObject:[NSNumber numberWithFloat:v->TexCoord[t]]];
    }

    [vertexdata addObjectsFromArray:position];
    [vertexdata addObjectsFromArray:color];
    [vertexdata addObjectsFromArray:texcord];

    return vertexdata;
}
+(…)阵列转换器:(顶点*)v
{
//将typeDef结构转换为单独的数组
NSMUTABLEARRY*位置=[[NSMUTABLEARRY alloc]init];
NSMUTABLEARRY*color=[[NSMUTABLEARRY alloc]init];
NSMutableArray*texcord=[[NSMutableArray alloc]init];
常量NSMutableArray*vertexdata=[[NSMutableArray alloc]init];
for(int p=0;position[p]];
}
对于(int c=0;cColor[c]];
}
for(int t=0;tTexCoord[t]];
}
[vertexdata addObjectsFromArray:位置];
[vertexdata addObjectsFromArray:color];
[vertexdata addObjectsFromArray:texcord];
返回顶点数据;
}


position)/sizeof(v))
-为什么
sizeof(v)
?你的意思是
sizeof(*v)
@H2CO3它给了我每个位置、颜色和texcoord的实际数字。哦,当你将位置、颜色和texcoord数组连接到一个生成的NSMutableArray中时,我忽略了这一部分。你为什么需要这个?
position)/sizeof(v))
-为什么
sizeof(v)
?你的意思是
sizeof(*v)
?(我仍然不明白这为什么有用,但无论如何…)@H2CO3它给出了每个位置、颜色和texcoord的实际数字。哦,当你将位置、颜色和texcoord数组连接到一个生成的NSMutableArray中时,我忽略了这一部分。为什么需要这个?你能用注释解释一下这个代码吗?什么是cstruct?顶点应该在哪里?好的@encode呢(struct-cstruct)?也应该用顶点替换吗?还有为什么是“&data”?我在那里得到一个错误,上面写着“@encode”类型“struct Vertex”不完整。使用valueWithPointer:method从指针获取NSValue是不正确的。请阅读apple的文档。此方法不会复制aPointer的内容,因此在NSValue对象存在时,您不能释放指针目标处的内存。()我怎样才能真正看到数字而不是内存地址?你能用注释解释一下这段代码吗?什么是cstruct?顶点应该在哪里?好的,@encode(struct cstruct)呢?它应该也替换为Vertex吗?以及为什么“&data”?我在那里得到一个错误,上面写着“@encode”类型“struct Vertex”不完整。使用valueWithPointer:method从指针获取NSValue是不正确的。请阅读apple的文档。此方法不会复制aPointer的内容,因此在NSValue对象存在时,您不能释放指针目标处的内存。()我如何才能真正看到数字而不是内存地址?我在“getBytes”处出错。无法将“Vertex”发送到类型为“void”的参数。[d getBytes:&v length:sizeof(v)];在这种情况下我没有收到错误。可能您忘记了参数v之前的符号(&)。我在“getBytes”处出错。无法将“Vertex”发送到类型为“void”的参数。[d getBytes:&v length:sizeof(v)];在这种情况下我没有发现错误。也许您忘记了参数v之前的符号(&)。正确!我想将我的TypeDef结构转换为一个数组。但我没有得到要传递的所有数据。只有第一行。您是否尝试替换这一行sizeof(v->Position)/sizeof(v)使用常量?我得到了与使用sizeof相同的结果。当你通过顶点{1,2,3}、{10,20,30,40}、{100200}时,结果数组中有什么?正确!我想将我的TypeDef结构转换为一个数组。但我没有得到所有要传递的数据。只有第一行。你是否尝试过替换这一行sizeof(v->Position)/sizeof(v)带有常数?我得到了与使用sizeof相同的结果。当通过顶点{{1,2,3},{10,20,30,40},{100200}时,结果数组中有什么?