如何创建嵌套数组或多维数组 [英] How to create nested array or multidimensional array
问题描述
我试图找出如何使用objective-c为iPhone SDK编程做嵌套数组或多维数组。
I am trying to figure out how to do either nested arrays or multidimensional array for iPhone SDK programming using objective-c.
我的数据是这样的, 3列和10到50行。
My data would be something like this, 3 columns and rows from 10 to 50.
name department year
John Sales 2008
Lisa Sales 2009
Gina Support 2007
任何帮助表示赞赏
推荐答案
我不确定这是你的例子还是你的术语,但这不是一个多维数组,这是一个记录数组。我会把你的每个记录都写成一个类:
I'm not sure whether it's your example or your terminology, but that's not really a multi-dimensional array, that's an array of records. I'd make each of your records a class:
@interface Employee
NSString* name;
NSString* dept;
NSString* year;
@end
@property (nonatomic,retain) NSString* name;
@property (nonatomic,retain) NSString* dept;
@property (nonatomic,retain) NSString* year;
// ... rest of class def
Employee* a = [[Employee alloc] init];
a.name = @"Bob";
a.dept = @"Sales";
a.year = @"2008";
// declare others
NSArray* array = [NSArray arrayWithObjects:a,b,c,nil];
[a release];
这比使用 struct $ c更客观C-ish $ c>。
如果你的确意味着一个多维数组,那么明显的方法就没有错。当然你可能想把它包装在一个类中,这样你也可以编写一些实用工具方法来使它更容易处理。
If you really do mean a multi-dimensional array then there's nothing wrong with the "obvious" approach. Of course you might want to wrap it up in a class so you can also write some utility methods to make it easier to deal with.
这篇关于如何创建嵌套数组或多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!