How to create a nested array or multidimensional array

I am trying to figure out how to make nested arrays or a multidimensional array for programming the iPhone SDK using objective-c.

My data will be something like this, 3 columns and rows from 10 to 50.

name     department     year
John     Sales          2008
Lisa     Sales          2009
Gina     Support        2007

Any help is appreciated

+1
source share
4 answers

I'm not sure if this is your example or your terminology, but this is not a very multi-dimensional array, it is an array of records. I would make each of your entries a class:

@interface Employee
  NSString* name;
  NSString* dept;
  NSString* year;
@end

@property (nonatomic,retain) NSString* name;
@property (nonatomic,retain) NSString* dept;
@property (nonatomic,retain) NSString* year;

// ... rest of class def


Employee* a = [[Employee alloc] init];
a.name = @"Bob";
a.dept = @"Sales";
a.year = @"2008";

// declare others

NSArray* array = [NSArray arrayWithObjects:a,b,c,nil];
[a release];

This is more Objective C-ish than use struct.

, "" . , , , .

+5

​​:

struct UserInfo
{
 string name;
 string department;
 int year;
}

struct UserInfo users[3];
users[0].name = "John";

etc....

, ? , !:)

+1

- Cocoa. NSArray objectAtRow:column:, . Caleb , , - .

+1

from @Stephen example ... You can do this to get the name of the first employee.

Employee *e = (Employee *) [array objectAtIndex:0];

NSLog(@"Name: %@", e.name);
0
source

All Articles