Re: How to store C Arrays as a Property or iVar?


Pascal Bourguignon
 


On 18 Aug 2017, at 21:07, Dave <dave@...> wrote:

Hi All,

I’ve converted some C code into Objective-C and have some C Array structures like so:

int [10][10]) myArray;

That I want to store in a Property or iVar inside a Wrapper Class. How do I do this?

I’ve tried:


@interface WrapperClass : NSObect
{
int [10][10]) mArray;
}

But I get an error if I try to assign or read it, as in:

myArray = _mArray;

or

_mArray =  myArray;


Yep.

Objective-C is C.

You have to know C to be able to program in Objective-C.

In C, arrays are not first class objects.  So you cannot use = to copy arrays (or to take a reference to an array, there’s no such notion in C).

The solution is to write a loop to copy the elements.

void copy_my_array(int src[10][10],int dst[10][10]){
    for(int i=0;i<10;++i){
        for(int j=0;j<10;++j){
            dst[i][j]=src[i][j];}}}

then use:

copy_my_array(myArray,_myArray);

-- 
__Pascal J. Bourguignon__



Join {cocoa@apple-dev.groups.io to automatically receive all group messages.