2

Im trying to encode into a NSValue an multidimensional array of structs.

This is the struct:

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...)

I need to encode into a NSValue to store it in the object "Block".

This is what I do to store:

Block *newBlock = [[Block alloc] init];
newBlock.blockVertices = [NSValue value:&Vertices withObjCType:@encode(Vertex)];

And to get it back:

Vertex tempVertices[24];
[newBlock.blockVertices getValue:&tempVertices];

Everything seems to work fine, then I try to compare values like this:

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(@" ");
}

And I'm getting weird results, like: -1.000000 = -1.998905

where could be the problem? Any help would be appreciated.

1 Answer 1

3

You need to tell @encode how many Vertex structs are being encoded, for example:

[NSValue value:&Vertices withObjCType:@encode(Vertex[24])];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.