如何在 iPhone 中创建数组的数组?

发布于 2024-10-14 20:23:30 字数 457 浏览 3 评论 0原文

我想创建一个嵌套数组或多维数组。

在我的数据中,

         FirstName   class   year   dept  lastName
         Bob          MBA    2000   Comp  Smith
         Jack         MS     2001   Comp  McDonald

         NSMutableArray *section = [[NSMutableArray alloc] init];  

我想将数据放入数组部分。

例如:

section[0] = [FirstName,LastName];

section[1] = [class, year, dept];

那么我怎样才能将这些值放入数组中呢? 请帮帮我。

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

白云悠悠 2024-10-21 20:23:30

我建议创建一个自定义数据存储类。您可以将其命名为 PDPerson.h 您还需要 .m 文件。对于每个属性,执行如下操作:

在 .h 中:像这样声明每个属性:

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

每次您想要创建新属性时在数组中添加“Person”,执行以下操作:

PDPerson *person = [[PDPerson alloc]init];

然后,您可以轻松地设置对象的属性,如下所示:

person.firstName = @"John";
person.lastName = @"Smith";
person.class = @"Math";
person.year = @"1995";
person.dept = @"Sciences";

并检索它们:

firstNameLabel.text = person.firstName;

这些对象的好处是,您现在要做的就是将 person 对象添加到数组中:

NSMutableArray *personArray = [[NSMutableArray alloc] init];
[personArray addObject:person];

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];
十级心震 2024-10-21 20:23:30
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"

请注意,这不是一种灵活的数据存储方式。考虑使用 CoreData 或创建您自己的类来表示数据。

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.

近箐 2024-10-21 20:23:30

您可以在一个 NSArray 中嵌套多个 NSArray 实例。

例如:

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

当来自 C++ 或 Java 等语言时,这可能看起来有些过大,在这些语言中,只需使用多个方括号即可创建多维数组。但这就是 Objective-C 和 Cocoa 的工作方式。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文