Как объявить массив C как свойство объекта Objective-C? - PullRequest
0 голосов
/ 11 октября 2009

У меня проблемы с объявлением массива C как свойства Objective-C (вы знаете @property и @synthesize, так что я могу использовать точечный синтаксис) ... Это всего лишь 3-мерный массив int ..

1 Ответ

5 голосов
/ 11 октября 2009

Вы не можете - массивы не являются lvalues ​​в C. Вы должны будете вместо этого объявить свойство указателя и полагаться на код, используя правильные границы массива, или вместо этого использовать свойство NSArray.

Пример:

@interface SomeClass
{
    int width, height, depth;
    int ***array;
}

- (void) initWithWidth:(int)width withHeight:(int)height withDepth:(int)depth;
- (void) dealloc;

@property(nonatomic, readonly) array;
@end

@implementation SomeClass

@synthesize array;

 - (void) initWithWidth:(int)width withHeight:(int)height withDepth:(int)depth
{
    self->width  = width;
    self->height = height;
    self->depth  = depth;
    array = malloc(width * sizeof(int **));
    for(int i = 0; i < width; i++)
    {
        array[i] = malloc(height * sizeof(int *));
        for(int j = 0; j < height; j++)
            array[i][j] = malloc(depth * sizeof(int));
    }
}

- (void) dealloc
{
    for(int i = 0; i < width; i++)
    {
        for(int j = 0; j < height; j++)
            free(array[i][j]);
        free(array[i]);
    }
    free(array);
}

@end

Тогда вы можете использовать свойство array как трехмерный массив.

...