iOS:为循环重命名NSString

时间:2013-03-13 12:06:34

标签: ios nsstring rename

NSString *string = @"Test";

是否可以重命名此字符串?我想在最后使用int使其动态化:

for (int i = 0; i <= 19; i++)
{
   // here the name of the string should be dynamic, so that I'll get 20 NSString (titled like string_00 to string_20)
}

1 个答案:

答案 0 :(得分:3)

无法在Objective C中引入新的局部变量。当您需要使用整数索引访问多个数据对象时,请使用数组或NSArray个对象:

NSString *strings[20];
for (int i = 0; i <= 19; i++) {
    strings[i] = [NSString stringWithFormat:@"Test%d", i];
}

NSMutableArray *strings = [NSMutableArray array];
for (int i = 0; i <= 19; i++) {
    [strings addObject:[NSString stringWithFormat:@"Test%d", i]];
}