初始化 2 个暗淡数组 NSMutableArray

发布于 2024-12-27 11:01:33 字数 490 浏览 0 评论 0原文

对于 CI 来说,会初始化一个这样的数组:

NSInteger x[3][10];那行得通。

下面我有一个可以工作的暗淡数组。想要将所有这些移动到 2 维数组中,如何初始化它?换句话说,采用下面的代码并使其适用于二维。

NSMutableArray *SRData;

SRData = [[NSMutableArray alloc] init];


NSMutableDictionary *SRRow;

SRRow = [[NSMutableDictionary alloc] init];

[SRRow setObject:@"Read" forKey:@"Descr"];
[SRRow setObject:@"Read2.png" forKey:@"Img"];

[SRRow setObject:@"Read the codes" forKey:@"Det"];

[SRData addObject:SRRow] ;   

[SRRow release];

For C I would init an array like this:

NSInteger x[3][10]; That works.

Below I have a one dim array that works. Would like to move all of this to a 2 dim array, How do I init it? So in other words take the code below and make it work with 2 dimensions.

NSMutableArray *SRData;

SRData = [[NSMutableArray alloc] init];


NSMutableDictionary *SRRow;

SRRow = [[NSMutableDictionary alloc] init];

[SRRow setObject:@"Read" forKey:@"Descr"];
[SRRow setObject:@"Read2.png" forKey:@"Img"];

[SRRow setObject:@"Read the codes" forKey:@"Det"];

[SRData addObject:SRRow] ;   

[SRRow release];

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

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

发布评论

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

评论(1

公布 2025-01-03 11:01:33

在 Objective-C 中,您只需拥有一个数组数组即可获取第二维。据我所知,没有速记,因此您只能执行以下操作:

NSMutableArray *firstDimension = [[NSMutableArray alloc] init];
for (int i = 0; i < rows; i++)
{
    NSMutableArray *secondDimension = [[NSMutableArray alloc] init];
    [firstDimension addObject:secondDimension];
}

因此您要做的就是将其他对象(在您的情况下为 NSMutableDictionarys)添加到 secondDimension 数组。用法如下:

[[firstDimension objectAtIndex:0] objectAtIndex:0];

编辑

完整代码示例:

NSMutableArray *SRData = [[NSMutableArray alloc] init]; //first dimension
NSMutableArray *SRRow = [[NSMutableArray alloc] init]; //second dimension
[SRData addObject:SRRow]; //add row to data
[SRRow release];

NSMutableDictionary *SRField = [[NSMutableDictionary alloc] init]; //an element of the second dimension
[SRField setObject:@"Read" forKey:@"Descr"];
//Set the rest of your objects

[SRRow addObject:SRField]; //Add field to second dimension
[SRField release];

现在,要获取该“字段”,您将使用如下代码:

[[SRData objectAtIndex:0] objectAtIndex:0]; //Get the first element in the first array (the second dimension)

In Objective-C, you just have to have an array of arrays to get the second dimension. To my knowledge, there is no shorthand, so you're stuck doing something like the following:

NSMutableArray *firstDimension = [[NSMutableArray alloc] init];
for (int i = 0; i < rows; i++)
{
    NSMutableArray *secondDimension = [[NSMutableArray alloc] init];
    [firstDimension addObject:secondDimension];
}

So all you would do is add your other objects (in your case, the NSMutableDictionarys) to the secondDimension array. Usage would be like:

[[firstDimension objectAtIndex:0] objectAtIndex:0];

Edit

Full code example:

NSMutableArray *SRData = [[NSMutableArray alloc] init]; //first dimension
NSMutableArray *SRRow = [[NSMutableArray alloc] init]; //second dimension
[SRData addObject:SRRow]; //add row to data
[SRRow release];

NSMutableDictionary *SRField = [[NSMutableDictionary alloc] init]; //an element of the second dimension
[SRField setObject:@"Read" forKey:@"Descr"];
//Set the rest of your objects

[SRRow addObject:SRField]; //Add field to second dimension
[SRField release];

Now, to get at that "field" you would use code such as the following:

[[SRData objectAtIndex:0] objectAtIndex:0]; //Get the first element in the first array (the second dimension)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文