title: How to make Grid class in Objective-C
body: <p>I'm trying to make a simple Grid class in Objective-C. I created a new Objective-C Class that inherits from NSObject. I'm having some trouble.</p>
  
  <p>I want to be able to do something like this:</p>
  
  <pre><code>Grid *grid = [[Grid alloc] initWithNumRows:5 numCols:5];
  int elem = grid[1][1];
  </code></pre>
  
  <p>However, the only way I know how to create a new grid class would be to create a NSMutableArray property in the Grid interface.</p>
  
  <pre><code>@interface Grid : NSObject
  @property int numRows;
  @property int numCols;
  @property (strong, nonatomic) NSMutableArray *grid;
  
  -(id) init;
  -(id) initWithNumRows:(int)numRows numCols:(int)numCols;
  @end
  </code></pre>
  
  <p>And in the implementation have:</p>
  
  <pre><code>-(id) initWithNumRows:(int)numRows numCols:(int)numCols
  {
      if ( (self = [super init]) )
      {
          self.numRows = numRows;
          self.numCols = numCols;
          self.grid = [[NSMutableArray alloc] init];
          for (int k = 0; k &lt; numRows; ++ k)
          {
              NSMutableArray* subArr = [[NSMutableArray alloc] init ];
              for (int s = 0; s &lt; numCols; ++ s)
              {
                  [subArr addObject:@0];
              }
              [self.grid addObject:subArr];
          }
      }
      return self;
  }
  </code></pre>
  
  <p>However, this would mean that if I wanted to use bracket notation, I would have to do this:</p>
  
  <pre><code>Grid *myG = [[Grid alloc] initWithNumRows:5 numCols:5];
  NSMutableArray *grid = myG.grid;
  int elem = grid[1][1];
  </code></pre>
  
  <p>This seems cumbersome and I feel like I'm missing something. Is there a way that I can just return the NSMutableArray grid in init?</p>
  
  <p>Thanks</p>