2d数组实例变量的Objective-c语法

时间:2012-04-25 15:12:04

标签: objective-c arrays memory-management syntax instance-variables

通常我会像Objective-c一样处理实例变量:

@interface MyClass : NSObject
@property (nonatomic, retain) NSMutableArray *mutableArray;
@end

@implementation MyClass
@synthesize mutableArray;

- (id) init {
    if((self = [super init])) {
        self.mutableArray = [NSMutableArray array];
    }
    return self;
}

- (void) dealloc {
    [mutableArray release];
    [super dealloc];
}

@end

我觉得上面的语法非常舒服。但是,对于像NSUInteger 2dArray[10][10]这样的2D数组实例变量的语法,我不太舒服。

关于接口声明,合成getter / setter和内存管理的2d数组实例变量的适当Objective-c语法是什么?

4 个答案:

答案 0 :(得分:3)

这不是Objective C语法。它是纯C语法。您不需要专门说您想要一个objc对象的2D数组。只需声明/定义可变数组并向其添加其他数组。

答案 1 :(得分:3)

您没有需要为您的阵列分配内存;它们在课堂上被定义得非常好,并且它们将始终以相同的大小存在。因此,您无需担心内存管理,应根据您的操作手动定义getter / setter。例如,这些getter / setter方法允许获取/设置单个值:

@interface MyClass : NSObject
{
    NSUInteger _twoDeeArray[10][10];
}

- (void)setTwoDeeArrayX:(NSUInteger)x y:(NSUInteger)y value:(NSUInteger)value;
- (NSUInteger)twoDeeArrayX:(NSUInteger)x y:(NSUInteger)y;

@end

@implementation MyClass

- (void)setTwoDeeArrayX:(NSUInteger)x y:(NSUInteger)y value:(NSUInteger)value
{
    _twoDeeArray[x][y] = value;
}

- (NSUInteger)twoDeeArrayX:(NSUInteger)x y:(NSUInteger)y
{
    return _twoDeeArray[x][y];
}

@end

你应该对x和y进行范围检查,但是你明白了。

答案 2 :(得分:1)

对于二维数组,您可以:

  1. 使用C数组(就像你在帖子中提到的那样)
  2. 将NSMutableArray添加到NSMutableArray
  3. 创建一个类来实现您的2D数组版本
  4. 如果你只想在数组中使用原始类型,那么这三种都是好的。

    对于Objective-C对象,您也可以使用id类型的C数组,但您必须自己管理内存分配/释放。 2和3是更好的方法。

    供参考:

答案 3 :(得分:0)

在iOS 6中,您可以使用下标来定义一个矩阵类,该矩阵类使用方括号语法matrix[row][col],您可以在其中存储对象,并且它们被矩阵正确保留,与使用C数组不同

首先创建一个Row对象,定义如下

- (id)initWithElementNumber:(NSUInteger)num {
    if (self = [super init]) {
        _row = [NSMutableArray arrayWithCapacity:num];
        for (int j = 0; j < num; j++)
            [_row addObject:[NSNull null]];
    }
    return self;
}

- (id)objectAtIndexedSubscript:(NSUInteger)idx {
    return self.row[idx];
}

- (void)setObject:(id)object atIndexedSubscript:(NSUInteger)idx {
    self.row[idx] = object;
}

@end

然后使用之前定义的Row类的Matrix类:

@implementation UKMatrix

- (id)initWithRows:(NSUInteger)numRows columsn:(NSUInteger)numCol {
    if (self = [super init])
    {
        _numCol = numCol;
        _numRows = numRows;
        _rows = [NSMutableArray arrayWithCapacity:numRows];
        for (int j = 0; j < numRows; j++)
            [_rows addObject:[[UKRow alloc] initWithElementNumber:numCol]];
    }
    return self;
}

- (id)objectAtIndexedSubscript:(NSUInteger)idx {
    return self.rows[idx];
}

- (NSString *)description {
    NSString *matrixDesc = @"";
    for (int j = 0; j < self.numRows; j++) {
        matrixDesc = [matrixDesc stringByAppendingString:@"\n"];
        for (int k = 0; k < self.numCol; k++)
            matrixDesc = [matrixDesc stringByAppendingFormat:@" %@ ",self[j][k]];
    }
    return matrixDesc;
}

@end

然后您可以使用以下语法

来使用Matrix
UKMatrix *matrix = [[UKMatrix alloc] initWithRows:4 columsn:2];
matrix[1][1] = @2;
NSLog(@"%@", matrix);