Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a two dimensional array in Objective-C

Tags:

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.

like image 810
draion Avatar asked Sep 11 '25 01:09

draion


2 Answers

If it doesn't need to be an object you can use:

float matrix[3][3];

to define a 3x3 array of floats.

like image 152
Ferruccio Avatar answered Sep 12 '25 16:09

Ferruccio


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

like image 43
Raxit Avatar answered Sep 12 '25 15:09

Raxit