简体   繁体   English

如何在iPhone中的运行时中创建NSMutableArrays

[英]how to create NSMutableArrays in runtime in iphone

In one of my applications I have to create the array's in run time whenever it's required. 在我的一个应用程序中,我必须在需要时在运行时创建数组。 As below: 如下:

for(int i=0;i<10;i++)
{
   NSMutableArray *array1 = [[NSMutableArray alloc] init];
} 

means instead of creating a single array like array1 I want to create the arrays based on i value. 意味着我不想创建像array1这样的单个数组,而是要基于i值创建数组。 Like whenever the condition is satisfied in for loop. 就像只要条件满足for循环。

New arrays would be like array0,array1,array2,array3,array4....etc 新的数组就像array0,array1,array2,array3,array4 .... etc

If anyone know please let me know. 如果有人知道,请告诉我。 thanks in advance. 提前致谢。

How about creating an array of arrays? 如何创建一个数组数组? Like: 喜欢:

NSMutableArray *array = [NSMutableArray arrayWithCapacity:10];
for (int i = 0; i < 10; i++) {
     [array addObject:[NSMutableArray array]];
}

//get array 0
NSMutableArray *array0 = [array objectAtIndex:0];

//get array 1
NSMutableArray *array1 = [array objectAtIndex:1];

//get array i

NSMutableArray *arrayI = [array objectAtIndex:i];

It sounds like you want to access a bunch of different arrays based on a naming convention: array"i". 听起来您想根据命名约定访问一堆不同的数组:array“ i”。 You can't really create dynamic variables like that, but you can create strings and store those arrays in a NSMutableDictionary for easy retrieval. 您不能真正创建像这样的动态变量,但是可以创建字符串并将这些数组存储在NSMutableDictionary以便于检索。

NSUInteger count = 10;
NSMutableDictionary *arrays = [NSMutableDictionary dictionaryWithCapacity:count];
for (int i = 0; i < count; i++){
    [arrays setObject:[[NSMutableArray alloc] init] forKey:[NSString stringWithFormat: @"array%i", i];
}

Now when you want to access a particular array you can use: 现在,当您要访问特定的阵列时,可以使用:

NSMutableArray *array1 = [arrays objectForKey:@"array1"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM