如何在 iPhone 中创建数组的数组?
我想创建一个嵌套数组或多维数组。
在我的数据中,
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我建议创建一个自定义数据存储类。您可以将其命名为
PDPerson.h
您还需要 .m 文件。对于每个属性,执行如下操作:在 .h 中:像这样声明每个属性:
然后在 .m 中:
每次您想要创建新属性时在数组中添加“Person”,执行以下操作:
然后,您可以轻松地设置对象的属性,如下所示:
并检索它们:
这些对象的好处是,您现在要做的就是将 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:
Then in the .m:
Each time you want to create a new "Person" in your array, do this:
You can then easily set the properties of the object like so:
And retrieve them:
The nice thing about these objects is that all you have to do now is add the person object to your array:
请注意,这不是一种灵活的数据存储方式。考虑使用 CoreData 或创建您自己的类来表示数据。
Be warned, this isn't a flexible way of storing data. Consider using CoreData or creating your own classes to represent the data.
您可以在一个 NSArray 中嵌套多个 NSArray 实例。
例如:
当来自 C++ 或 Java 等语言时,这可能看起来有些过大,在这些语言中,只需使用多个方括号即可创建多维数组。但这就是 Objective-C 和 Cocoa 的工作方式。
You can just nest multiple NSArray instances within an NSArray.
For example:
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.