本文介绍了Objective-C在循环中创建变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以在循环内创建变量.基本上像这样,除了变量variable1,variable2和variable3将存在.

Is there a way to create variables inside of a loop. Basically something like this, except that the variables variable1, variable2 and variable3 would exist.

int x;

for (x = 1; x < 4; x++) {
   int variable[x];
   variable[x] = x;
}

推荐答案

没有,没有.

但是您可以执行以下操作:

But you can do something like this:

NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
for (int i = 0; i < 4; i++) {
    [dictionary setObject:[NSNumber numberWithInt:i] forKey:[NSString stringWithFormat:@"%i", i]];
}

这会将您的x保存在NSMutableDictionary中,这与其他语言中的关联数组相当.

This will save your xs in an NSMutableDictionary, which is comparable to an associative array in other languages.

这篇关于Objective-C在循环中创建变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 19:50