1

通常、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

上記の構文でかなり快適に感じます。ただし、のような2D配列インスタンス変数の構文についてはあまり快適ではありませんNSUInteger 2dArray[10][10]

インターフェイス宣言、ゲッター/セッターの合成、およびメモリ管理に関して、2d配列インスタンス変数の適切なObjective-c構文は何ですか?

4

4 に答える 4

3

これは Objective C の構文ではありません。これは純粋な C 構文です。objc オブジェクトの 2D 配列が必要だと言う必要はありません。可変配列を宣言/定義し、他の配列をそれに追加するだけです。

于 2012-04-25T15:19:14.597 に答える
3

配列にメモリを割り当てる必要はありません。それらはクラスで定義されていてもまったく問題なく、常に同じサイズで存在します。したがって、メモリ管理について心配する必要はありません。やりたいことに応じて、ゲッター/セッターを手動で定義する必要があります。たとえば、これらのゲッター/セッター メソッドにより、個々の値を取得/設定できます。

@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 の範囲チェックが必要ですが、アイデアはわかります。

于 2012-04-25T15:25:09.003 に答える
1

2 次元配列の場合、次のことができます。

  1. C配列を使用します(投稿で言及したように)
  2. NSMutableArray を NSMutableArray に追加する
  3. 2D 配列のバージョンを実装するクラスを作成します

配列でプリミティブ型を使用したいだけなら、3 つすべてが適切です。

Objective-C オブジェクトの場合、C 配列のid型を使用することもできますが、メモリの割り当て/割り当て解除を自分で管理する必要があります。2と3はこれを行うためのより良い方法です。

ご参考までに:

于 2012-04-25T15:27:54.370 に答える
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

次に、次の構文でマトリックスを使用できます

UKMatrix *matrix = [[UKMatrix alloc] initWithRows:4 columsn:2];
matrix[1][1] = @2;
NSLog(@"%@", matrix);
于 2013-03-17T14:58:50.037 に答える