我目前有一个循环,它遍历 NSString 对象的 NSArray。我希望在循环的每次迭代中创建一个 NSString 变量,使用当前评估的 NSString 对象的字符串值(来自 NSArray)作为变量的名称。这可能通过示例得到最好的解释:
for (i = 0; i < [arrayOfStrings count]; i++) {
// NSString *<name of variable is [arrayOfStrings objectAtIndex:i]> = [[NSString alloc] init];
}
有办法完成这个任务吗?我正在使用 iPhone SDK 3.1。谢谢。
最佳答案
不完全是,但您可以使用 NSMutableDictionary 在运行时添加键/值对,其中键是字符串数组中的名称。请参阅http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/Foundation/Classes/NSMutableDictionary_Class/Reference/Reference.html .
关于iphone - 在 Objective-C 中运行时创建变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2166741/