Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-C. Property for C array

I need something like this:

@property (nonatomic, retain) int field[10][10];

but this code doesn't work. How to replace it? I need both setter and getter methods

like image 852
Gargo Avatar asked Dec 26 '22 00:12

Gargo


1 Answers

You can do it if you wrap the array in a struct. Structs are supported in @property notation (see CGRect bounds on CALayer, for example).

First define your struct:

typedef struct {
    int contents[10][10];
} TenByTenMatrix;

Then, in your class interface, you can do:

@property (assign) TenByTenMatrix field;

Note that in this case, you can only get or set the whole array using the property. So you can't do

self.field.contents[0][0] = 1;

You'd have to do

TenByTenMatrix temp = self.field;
temp.contents[0][0] = 1;
self.field = temp;
like image 190
joerick Avatar answered Jan 09 '23 18:01

joerick