Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Usually I treat instance variables in Objective-c like this:

@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

I feel pretty comfortable w/ the above syntax. However I'm not so comfortable w/ the syntax for a 2D array instance variable like NSUInteger 2dArray[10][10].

What's the appropriate Objective-c syntax for a 2d array instance variable with regards to interface declaration, synthesizing getters/setters and memory management?

share|improve this question

4 Answers 4

up vote 3 down vote accepted

You don't need to allocate memory for your array; they are perfectly fine being defined in the class and they will always exist, at the same size. You therefore don't need to worry about memory management and your getter/setters should be defined manually, depending on what you want to do. For example these getter/setter methods allow getting/setting an individual value:

@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

You should probably have range-checking for x and y, but you get the idea.

share|improve this answer
1  
You have some extra commas in you method definitions! –  mttrb Apr 25 '12 at 15:29
    
Cleaned up the extra commas –  MrDatabase Apr 25 '12 at 18:24
    
@MrDatabase Cheers. –  trojanfoe Apr 26 '12 at 13:45

That's not an Objective C syntax. It's pure C syntax. You don't need to exclusively say that you want a 2D array of objc objects. Just declare/define the mutable array and add other arrays to it.

share|improve this answer

For two-demensional arrays, you can:

  1. Go with C arrays (like what you mentioned in the post)
  2. Add NSMutableArray into NSMutableArray
  3. Create a class to implement your version of 2D-array

If you just want to use primitive types in your array, all three are good.

For Objective-C objects, you can also go with C array with id type but you have to manage memory allocation/deallocation yourself. 2 and 3 are better way to do this.

FYI:

share|improve this answer

in iOS 6 you can use subscript to define a matrix class that uses the square bracket syntax matrix[row][col] where you can store objects and they are correctly retained by the matrix, differently than using a C array

First create a Row object, defined like this

- (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

And then a Matrix class that uses the Row class previously defined:

@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

then you can use the Matrix with the following syntax

UKMatrix *matrix = [[UKMatrix alloc] initWithRows:4 columsn:2];
matrix[1][1] = @2;
NSLog(@"%@", matrix);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.