我正在尝试将多维结构数组编码到 NSValue 中。
这是结构:
typedef struct {
float Position[3];
float Color[4];
} Vertex;
const Vertex Vertices[] = {
// Front
{{1, -1, 0}, {1, 0, 0, 1}},
{{1, 1, 0}, {1, 0, 0, 1}},
(etc...)
我需要编码为 NSValue 以将其存储在对象“Block”中。
这就是我要存储的内容:
Block *newBlock = [[Block alloc] init];
newBlock.blockVertices = [NSValue value:&Vertices withObjCType:@encode(Vertex)];
并取回它:
Vertex tempVertices[24];
[newBlock.blockVertices getValue:&tempVertices];
一切似乎都工作正常,然后我尝试比较这样的值:
for (int i = 0; i < 24; i++){
NSLog(@"%f = %f", Vertices[i].Position[0], tempVertices[i].Position[0]);
NSLog(@"%f = %f", Vertices[i].Position[1], tempVertices[i].Position[1]);
NSLog(@"%f = %f", Vertices[i].Position[2], tempVertices[i].Position[2]);
NSLog(@" ");
}
我得到了奇怪的结果,例如: -1.000000 = -1.998905
问题可能出在哪里?任何帮助将不胜感激。
最佳答案
您需要告诉@encode
有多少Vertex
结构正在被编码,例如:
[NSValue value:&Vertices withObjCType:@encode(Vertex[24])];
关于iphone - 编码为 NSValue 结构数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11062702/