Reputation: 9390
I want to create a nested array or multidimensional array.
In my data is,
FirstName class year dept lastName
Bob MBA 2000 Comp Smith
Jack MS 2001 Comp McDonald
NSMutableArray *section = [[NSMutableArray alloc] init];
I want to put my data into the section Array.
Eg:
section[0] = [FirstName,LastName];
section[1] = [class, year, dept];
So how can i put the values into array like that. Please help me out.
Thanks
Upvotes: 0
Views: 941
Reputation: 9126
I would recommend creating a custom data storage class. You could call it PDPerson.h
You'll also need the .m file. For each property, do something like this:
In the .h: Declare each of your properties like so:
@interface PDPerson : NSObject{
}
@property(nonatomic, retain) NSString *firstName;
@property(nonatomic, retain) NSString *lastName;
@property(nonatomic, retain) NSString *class;//May want to consider renaming
@property(nonatomic, retain) NSString *year;
@property(nonatomic, retain) NSString *dept;
@end
Then in the .m:
@implementation
@synthesize firstName, lastName;
@synthesize class, year dept;
-(void)dealloc{
[firstName release];
[lastName release];
[class release];
[year release];
[dept release];
}
Each time you want to create a new "Person" in your array, do this:
PDPerson *person = [[PDPerson alloc]init];
You can then easily set the properties of the object like so:
person.firstName = @"John";
person.lastName = @"Smith";
person.class = @"Math";
person.year = @"1995";
person.dept = @"Sciences";
And retrieve them:
firstNameLabel.text = person.firstName;
The nice thing about these objects is that all you have to do now is add the person object to your array:
NSMutableArray *personArray = [[NSMutableArray alloc] init];
[personArray addObject:person];
Upvotes: 2
Reputation: 28982
You can just nest multiple NSArray instances within an NSArray.
For example:
NSMutableArray* sections = [[NSMutableArray alloc] init];
for (int i = 0; i < numberOfSections; i++)
{
NSMutableArray* personsInSection = [[NSMutableArray alloc] init];
[sections insertObject:personsInSection atIndex:i];
for (int x = 0; x < numberOfPersons; x++)
{
Person* person = [[Person alloc] init];
[personsInSection insertObject:person atIndex:x];
}
}
This may seem like overkill when coming from languages such as C++ or Java, where multidimensional arrays can be created simply by using multiple sequare brackets. But this is way things are done with Objective-C and Cocoa.
Upvotes: 0
Reputation: 11920
NSArray *section1 = [NSArray arrayWithObjects: @"1,1", @"1,2", @"1,3", nil];
NSArray *section2 = [NSArray arrayWithObjects: @"2,1", @"2,2", @"2,3", nil];
NSArray *section3 = [NSArray arrayWithObjects: @"3,1", @"3,2", @"3,3", nil];
NSArray *sections = [NSArray arrayWithObjects: section1, section2, section3, nil];
int sectionIndex = 1;
int columnIndex = 0;
id value = [[sections objectAtIndex:sectionIndex] objectAtIndex:columnIndex];
NSLog(@"%@", value); //prints "2,1"
Be warned, this isn't a flexible way of storing data. Consider using CoreData or creating your own classes to represent the data.
Upvotes: 0