2
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)
}
4

1 回答 1

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]];
}
于 2013-03-13T12:08:05.957 回答