0

I have a custom object with two 2D arrays as follows:

float a[5][5];
float b[5][10];

And I want to encode this object:

- (void)encodeWithCoder:(NSCoder *)coder
{
    // Encode a
    // Encode b
}

How can I encode and decode these 2D arrays? I couldn't find an appropriate method to do this operation. Thanks in Advance,

3 Answers 3

3

You can create a struct with those:

typedef struct myStruct {

    float a[5][5];
    float b[5][10];

}MyStruct;

Then use NSValue to encode them as objects.

MyStruct i;
memset(&i, 0, sizeof(i));
//Set values on struct

NSValue *v = [NSValue valueWithBytes:&i objCType:@encode(MyStruct)];

Now, you can treat your struct as an object and use then with NSArrays and so on.

To read the values you can use:

MyStruct i;

[v getValue:&i];

I did it like this to work with streams and legacy servers and it worked smoothly.

Hope it helps.

Sign up to request clarification or add additional context in comments.

Comments

1

Stick them into objective-c containers for the encoding/decoding process.

Comments

1

There is no pre-built method for that - you would need to write it yourself. The simplest way would be to write two ints first for the number of rows and the number of columns, and then write all R*C elements one by one.

Decoding is equally simple - read the two ints for R and C, and then run two nested loops reading the floats.

Define two helper functions for that to avoid copy-pasting the same code twice.

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.