Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 通过NSString值创建NSMutableArray_Objective C_Nsstring_Nsmutablearray - Fatal编程技术网

Objective c 通过NSString值创建NSMutableArray

Objective c 通过NSString值创建NSMutableArray,objective-c,nsstring,nsmutablearray,Objective C,Nsstring,Nsmutablearray,如何通过NSString值动态创建NSMutableArray(名称) e.g. NSString *stringName = @"helloArray1"; 然后使用“helloArray1”动态创建NSMutableArray e.g. NSMutableArray * (--here the stringName / helloArray1---) = [NSMutableArray new]; 然后是NSLog: NSLog(@"%@", (--here the stringName

如何通过NSString值动态创建NSMutableArray(名称)

e.g. NSString *stringName = @"helloArray1";
然后使用“helloArray1”动态创建NSMutableArray

e.g. NSMutableArray * (--here the stringName / helloArray1---) = [NSMutableArray new];
然后是NSLog:

NSLog(@"%@", (--here the stringName / helloArray1---) );

谢谢你的帮助,我想那是不可能的

更好的方法是动态地设置所有对象。例如:

NSMutableDictionary *variables = [NSMutableDictionary new];
for (int i = 0; i < 10; i++) {
    NSMutableArray *temp = [[NSMutableArray alloc] initWithObjects:@"1", @"2", nil];
    [variables setObject:temp forKey:[NSString stringWithFormat:@"value_%d", i]];
}

NSLog(@"%@", [variables objectForKey:@"value_1"]);
NSMutableDictionary*变量=[NSMutableDictionary new];
对于(int i=0;i<10;i++){
NSMUTABLEARRY*temp=[[NSMUTABLEARRY alloc]initWithObjects:@“1”,“2”,nil];
[variables setObject:temp-forKey:[NSString stringWithFormat:@“value_U%d”,i]];
}
NSLog(@“%@,[variables objectForKey:@“value_1]”);

您的意思是要动态创建具有特定名称的变量?那是不可能的;但如果您想用某个名称填充现有实例变量,有一种方法可以做到这一点。一种可能的方法是使用字典,其中数组将是
stringName
key的值…@DarkDust:是否编写示例代码以使用特定名称填充现有实例变量?@Alladinian:是否编写示例代码?@Daniela:搜索“key-value-Coding”,Objective-C中的一个重要概念:您通常可以执行
[self-setValue:someObject-forKey:@“instanceVariableName”]