Creating a two dimensional array in Objective-C
Whats the easiest way to declare a two dimensional array in Objective-C? I am reading an matrix of numbers from a text file from a website and want to take the data and place it into a 3x3 matrix.
Once I read the URL into a string, I create an NSArray and use the componentsSeparatedByString method to strip of the carriage return line feed and create each individual row. I then get the count of the number of lines in the new array to get the individual values at each row. This will give mw an array with a string of characters, not a row of three individual values. I just need to be able to take these values and create a two dimensional array.
If it doesn't need to be an object you can use:
float matrix[3][3];
to define a 3x3 array of floats.
You can use the Objective C style array.
NSMutableArray *dataArray = [[NSMutableArray alloc] initWithCapacity: 3];
[dataArray insertObject:[NSMutableArray arrayWithObjects:@"0",@"0",@"0",nil] atIndex:0];
[dataArray insertObject:[NSMutableArray arrayWithObjects:@"0",@"0",@"0",nil] atIndex:1];
[dataArray insertObject:[NSMutableArray arrayWithObjects:@"0",@"0",@"0",nil] atIndex:2];
I hope you get your answer from the above example.
Cheers, Raxit
This will also work:
NSArray *myArray = @[
@[ @1, @2, @3, @4],
@[ @1, @2, @3, @4],
@[ @1, @2, @3, @4],
@[ @1, @2, @3, @4],
];
In this case it is a 4x4 array with just numbers in it.
I'm not absolutely certain what you are looking for, but my approach to a two dimensional array would be to create a new class to encapsulate it. NB the below was typed directly into the StackOverflow answer box so it is not compiled or tested.
@interface TwoDArray : NSObject
{
@private
NSArray* backingStore;
size_t numRows;
size_t numCols;
}
// values is a linear array in row major order
-(id) initWithRows: (size_t) rows cols: (size_t) cols values: (NSArray*) values;
-(id) objectAtRow: (size_t) row col: (size_t) col;
@end
@implementation TwoDArray
-(id) initWithRows: (size_t) rows cols: (size_t) cols values: (NSArray*) values
{
self = [super init];
if (self != nil)
{
if (rows * cols != [values length])
{
// the values are not the right size for the array
[self release];
return nil;
}
numRows = rows;
numCols = cols;
backingStore = [values copy];
}
return self;
}
-(void) dealloc
{
[backingStore release];
[super dealloc];
}
-(id) objectAtRow: (size_t) row col: (size_t) col
{
if (col >= numCols)
{
// raise same exception as index out of bounds on NSArray.
// Don't need to check the row because if it's too big the
// retrieval from the backing store will throw an exception.
}
size_t index = row * numCols + col;
return [backingStore objectAtIndex: index];
}
@end
First you to have set An NSMutableDictionary on .h file
@interface MSRCommonLogic : NSObject
{
NSMutableDictionary *twoDimensionArray;
}
then have to use following functions in .m file
- (void)setValuesToArray :(int)rows cols:(int) col value:(id)value
{
if(!twoDimensionArray)
{
twoDimensionArray =[[NSMutableDictionary alloc]init];
}
NSString *strKey=[NSString stringWithFormat:@"%dVs%d",rows,col];
[twoDimensionArray setObject:value forKey:strKey];
}
- (id)getValueFromArray :(int)rows cols:(int) col
{
NSString *strKey=[NSString stringWithFormat:@"%dVs%d",rows,col];
return [twoDimensionArray valueForKey:strKey];
}
- (void)printTwoDArray:(int)rows cols:(int) cols
{
NSString *strAllsValuesToprint=@"";
strAllsValuesToprint=[strAllsValuesToprint stringByAppendingString:@"\n"];
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
NSString *strV=[self getValueFromArray:row cols:col];
strAllsValuesToprint=[strAllsValuesToprint stringByAppendingString:[NSString stringWithFormat:@"%@",strV]];
strAllsValuesToprint=[strAllsValuesToprint stringByAppendingString:@"\t"];
}
strAllsValuesToprint= [strAllsValuesToprint stringByAppendingString:@"\n"];
}
NSLog(@"%@",strAllsValuesToprint);
}