gpt4 book ai didi

iOS:为循环重命名 NSString

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:11:08 24 4
gpt4 key购买 nike

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

最佳答案

在 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]];
}

关于iOS:为循环重命名 NSString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15384866/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com