Ever felt the need to use a two dimensional array in you iOS project? Well, I have. So here is what I had done:
- Created a NSObject subclass.
- Taken two private NSMutableArray instances, one for row and one for column.
- Created a nested for loop to traverse those arrays, and fill them with NSNull objects.
- Created a method to insert value/object on the [row][column] index.
So this is fairly easy and straightforward. But wouldn’t it be nicer if you didn’t have to do all these steps and figure out how to write those monotonous codes all by yourself?
My good friend, I have good news for you! I have created a simple CocoaPod to minimize your task!
To install it, simply add the following line to your Podfile:
pod 'SHTwoDimensionalArray'
and run
pod install
command!
To use this, try the following code snippet:
#import "SHTwoDimensionalArray.h" SHTwoDimensionalArray *twoDArray = [SHTwoDimensionalArray arrayWithRows:2 andColumns:2]; [twoDArray setObject:@"Foo" inRow:0 column:0]; ... NSString *foo = [twoDArray objectInRow:0 column:0];
I hope you find it helpful and it’s under MIT license. Feel free to fork, modify and contribute!
Cheers!