如何在iPhone中创建Array of Array? [英] How to create Array of Array in iPhone?
问题描述
我想创建一个嵌套数组或多维数组.
I want to create a nested array or multidimensional array.
在我的数据中,
FirstName class year dept lastName
Bob MBA 2000 Comp Smith
Jack MS 2001 Comp McDonald
NSMutableArray *section = [[NSMutableArray alloc] init];
我想将数据放入Array部分.
I want to put my data into the section Array.
例如:
section[0] = [FirstName,LastName];
section[1] = [class, year, dept];
那么我该如何将这些值放入数组中呢? 请帮帮我.
So how can i put the values into array like that. Please help me out.
谢谢
推荐答案
我建议创建一个自定义数据存储类.您可以将其命名为PDPerson.h
.您还需要.m文件.对于每个属性,请执行以下操作:
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:
在.h 中:这样声明每个属性:
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
然后在.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";
并检索它们:
firstNameLabel.text = person.firstName;
关于这些对象的好处是,您现在要做的就是将person对象添加到您的数组中:
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];
这篇关于如何在iPhone中创建Array of Array?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!