1

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

My data would 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

Matthew Groves
  • 25,181
  • 9
  • 71
  • 121
  • One note: you don't want nested NSArrays because of the way the class use to verify the presence of an object in the collection: When you use containsObject: NSArray sends an -hash message to each object in the collection and compares the result with the object whose presence you are checking. When an object is another collection, the default implementation sends hash to the collection that in turn sends it to each object inside it. So if you need a fast and efficient matrix you need to roll you own. – Giuliano Galea May 17 '11 at 09:46

4 Answers4

5

I'm not sure whether it's your example or your terminology, but that's not really a multi-dimensional array, that's an array of records. I'd make each of your records 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 using a struct.

If you really do mean a multi-dimensional array then there's nothing wrong with the "obvious" approach. Of course you might want to wrap it up in a class so you can also write some utility methods to make it easier to deal with.

Stephen Darlington
  • 51,577
  • 12
  • 107
  • 152
  • Thank you all for your help. I tried using this approach, but how would you retrieve the values back? Employee* a = [[Employee alloc] init]; a.name = @"Bob"; a.dept = @"Sales"; a.year = @"2008"; Employee* b = [[Employee alloc] init]; b.name = @"Gina"; b.dept = @"Support"; b.year = @"2007"; NSString *yourData = [array objectAtIndex:indexPath.row]; The above line yourData causes an error. How should I retrieve values for example the Year value for row two? –  Jul 14 '09 at 23:19
  • What's in your .m file? Did you add a @synthesize line for each property? – Stephen Darlington Jul 15 '09 at 07:21
  • Yes I do have @synthesize in my .m file and I have the above code in the same .m file. The .h file has the @property. –  Jul 16 '09 at 01:25
1

Could you perhaps make a structure like this:

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

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

etc....

and make an array out of the structure so you wouldn't have to deal with multidimensional arrays? If not then just ignore me! :)

CalebHC
  • 4,998
  • 3
  • 36
  • 42
  • I agree, this is a much cleaner solution than using arrays of arrays. – amischiefr Jul 14 '09 at 14:59
  • 2
    In order to do this in Objective-C, you'd need to make it an object rather than a struct (as structs cannot do proper memory management of their contents). – Chuck Jul 14 '09 at 15:06
1

Nesting arrays is the only really practical way to do a multidimensional array in Cocoa. You could define a category on NSArray with methods like objectAtRow:column: to make accessing items more convenient. Though in your case, I agree with Caleb that it doesn't look like you need this — your data looks like an array of objects.

Chuck
  • 234,037
  • 30
  • 302
  • 389
0

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

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

NSLog(@"Name: %@", e.name);
Nafiz
  • 462
  • 4
  • 17