ios - C-Style 2D Array as ivar -
in c, following create 2d array:
int intarray[10][10];
in c99, create vla:
size_t col = 10; size_t row = 10; int array[row][col];
within method in objective-c, can create 2d array hold id
s follows:
id genobjectarray[10][10];
is possible create 2d array ivar in objective-c?
the following have tried:
@interface myclass () { id objarray[][]; //this doesn't work, unless specific size. //i want this, specific size later during //runtime }
in c, following , allocate space 2d array later within block scope:
int **array; int *elements;
i can same within objective-c, too, problem arises when use id
or other object types; other words, following not valid:
id **array; id *elements;
thus, question is, possible declare c-style 2d array ivar holds id
s?
i understand achieve using normal ns(mutable)array
; serves educational purposes.
you can't this. c99 vla, space required array allocated @ point array declared. ivar, analogous time when object allocated , initialized, there's no support in objective c that. you'd need have stronger definition of object constructor can (more java's constructors objective c's initializers).
the closest can this:
@interface myclass () { id * objarray; } -(instancetype)initwithrow:(size_t)row col:(size_t)col { self = [super init]; if (self) { objarray = calloc(row * col * sizeof(id)); } return self; } -(void)dealloc { free(objarray); }
in case, you're declaring ivar pointer , managing storage (and stride, multi-dimensional array).
obviously, nsarray
better in possible ways.
Comments
Post a Comment