客观的C 2维数组? [英] objective c 2-dimensional array?
问题描述
我有一点麻烦创建一个动态分配的二维数组。如果它的确与众不同,我编码IOS。
I am having a bit of trouble creating a dynamically allocated two-dimensional array. If it makes a difference I am coding for ios.
我有点新的客观c和该语言的内存分配比较混乱。我做了一些研究,并拿出的NSMutableArray而这个链接,目标C 二维数组一>,但我仍然感到困惑。
I am a bit new to objective c and the memory allocation in this language is a bit confusing. I did a bit of research and come up with NSMutableArray and this link, 2d arrays in objective c, but am still confused.
我所希望做的是创造行和colunms的列表,包含列对象的动态创建的数字每一行。行数也是动态的。
What I am looking to do is create a list of rows and colunms, each row containing a dynamically created number of column objects. The number of rows is also dynamic.
如果您想布局作为哈希映射;图形,每个散列细胞将包含一个按钮,并在屏幕上显示的标签
If you think of the layout as a a hash map; graphically, each hash cell will contain a button and a label displayed on the screen.
编辑:
我有,
NSMutableArray *rows;
NSMutableArray *columns;
- (void)viewDidLoad {
rows = [[NSMutableArray alloc] init];
columns = [[NSMutableArray alloc] init];
//allocate memory?
}
我的主要问题是如何分配的内存为每个条目?
My main question is how to allocate the memory for each entry?
EDIT2:
如果有人需要帮助,这我得到了答案,并期待更多的字典,而不是数组,他们有很多有用的方法。
If anyone needed help with this I got the answer and look more to dictionaries instead of Arrays, they have lots of helpful methods.
推荐答案
您不能创建Objective-C的2维数组,但你可以使用C风格的数组,例如:
You cannot create 2 dimensional array with objective-c but you can use c style array, for example:
@implementation TwoDimCArray
{
NSUInteger _array[8][8];
}
- (id)init
{
if (self = [super init]){
[self clearArray];
}
return self;
}
- (int)cellStateAtColumn:(NSInteger)column andRow:(NSInteger)row
{
return _array[column][row];
}
- (void)setCellState:(BoardCellState)state forColumn:(NSInteger)column andRow:(NSInteger)row
{
_array[column][row] = state;
}
- (void) clearArray
{
memset(_array, 0, sizeof(NSUInteger) * 8 * 8);
}
@end
当你嵌入到另一个NSArray的NSArray中进行比较它的工作原理非常快。
希望它帮助。
It works very quick when you compare it with NSArray embedded in another NSArray. Hope it help.
这篇关于客观的C 2维数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!