How to create Array of Array in iPhone?

若如初见. 提交于 2020-01-06 13:10:51

问题


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


回答1:


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];



回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/4815362/how-to-create-array-of-array-in-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!