简体   繁体   English

轻松创建一百万个具有唯一名称的NSMutableArrays

[英]Create 1 million NSMutableArrays with unique names easily

After teaching myself (mostly from this website) I have finally been unable to find a solution to a problem. 在自学之后(主要是从该网站),我终于无法找到解决问题的方法。
I am trying to easily create 1 million NSMutableArrays with unique names. 我正在尝试轻松创建具有唯一名称的一百万个NSMutableArrays。 What I mean by easily is not having 'hard code' all of the individual arrays. 我的意思是,不必对所有单独的数组都进行“硬编码”。
What I would like to do is something like this: 我想做的是这样的:

for (int i = 1; i <= 1000000; i++) {  
    NSString *arrayNumber = [NSString stringWithFormat:@"%d", i];  
    NSString *millionArrayNumber = @"millionArrayNumber";  
    NSString *arrayName = [millionArrayNumber stringByAppendingString:arrayNumber];  
    NSMutableArray *[NSString arrayName] = [NSMutableArray array];  
}  

I can understand why this doesn't work but I can't think of another way of doing it. 我能理解为什么这行不通,但我想不出另一种方式。 I thought it might be possible to use something similar to: 我认为可能可以使用类似于以下内容的东西:

[button setTag = 1];

If I change the code to: 如果我将代码更改为:

for (int i = 1; i <= 1000000; i++) {    
    NSMutableArray *millionArray = [NSMutableArray array];  
    [millionArray setTag: i];
}

I would then use the tag to control the arrays as you can do with buttons. 然后,我将像使用按钮一样使用标记来控制数组。 This doesn't work for arrays though. 但是,这不适用于数组。
I hope I haven't been ambiguous. 我希望我没有模棱两可。

I'm not sure how to convert a string into literal code like you're doing, but I think it's possible. 我不确定如何像您所做的那样将字符串转换为文字代码,但是我认为这是可能的。

However, you can always just kick 1 million arrays to an NSMutableDictionary and give each one your unique key name. 但是,您始终可以将一百万个数组踢到NSMutableDictionary,并为每个数组指定唯一的键名。

NSMutableDictionary *arrayStore = [NSMutableDictionary dictionary];
for (int i = 1; i <= 1000000; i++) {  
    NSString *arrayNumber = [NSString stringWithFormat:@"%d", i];  
    NSString *millionArrayNumber = @"millionArrayNumber";  
    NSString *arrayName = [millionArrayNumber stringByAppendingString:arrayNumber];  
    arrayStore[arrayName] = [NSMutableArray array];  
}

And obviously to get whatever array you want later, you just use: 显然,要以后获得所需的任何数组,只需使用:

NSMutableArray *uniqueArray = arrayStore[arrayName];

Edit: Not sure if you want to keep the number as the unique key as mentioned in the comment below but if so, you can do it with this: 编辑:不确定是否要保留数字作为唯一的密钥,如下面的注释中所述,但是如果是这样,则可以这样做:

NSMutableDictionary *arrayStore = [NSMutableDictionary dictionary];
for (int i = 1; i <= 1000000; i++) { 
   arrayStore[@(i)] = [NSMutableArray array];  
}

And access it with (for example, array number 100): 并使用(例如,数组编号100)访问它:

NSMutableArray *uniqueArray = arrayStore[@100];

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

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