如何将C数组声明为Objective-C对象的属性?

Rex*_*ids 0 c arrays iphone uikit nsobject

我无法将C数组声明为Objective-C属性(你知道@property和@synthesize所以我可以使用点语法)......它只是一个3维int数组..

Ada*_*eld 5

你不能 - 数组不是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
Run Code Online (Sandbox Code Playgroud)

然后,您可以将该array属性用作三维数组.