gpt4 book ai didi

ios - 在 Objective C 中连接整数数组中的字符串

转载 作者:行者123 更新时间:2023-11-29 10:45:53 25 4
gpt4 key购买 nike

我希望在 int 类型数组中的每个对象的末尾添加一个字符串。

_finalQuestionArray 是 8 值整数数组 [2, 3, 4, 1, 2, 3, 4, 1]

_card1Type 是一个字符串@"o"

_card1Type 是一个字符串@"p"

我想创建一个新的字符串数组,看起来像 [2o, 3o, 4o, 1o, 2p, 3p, 4p, 1p] 但我卡住了。如何将字符串连接到数组中的整数?

到目前为止,这是我的代码://此函数在此处附加与卡类型对应的字母

for (int i=0; i<[_finalQuestionArray count]; i++){

if(i<([_finalQuestionArray count]/2)){

[[_finalQuestionArray objectAtIndex:i]stringByAppendingString:_card1Type];

} else {

[[_finalQuestionArray objectAtIndex:i]stringByAppendingString:_card2Type];

}

}

最佳答案

这是您要找的吗?

如果您通过示例学习,请检查以下内容,

NSMutableArray *finalArray = [[NSMutableArray alloc] init];

NSString *cardType1 = @"o";
NSStrind *cardType2 = @"p";

NSArray *array = @[ @2, @3, @4, @1, @2, @3, @4, @1];
[array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {

if (idx < (array.count / 2)) {
[finalArray addObject:[NSString stringWithFormat:@"%@%@", obj, cardType1]];
} else {
[finalArray addObject:[NSString stringWithFormat:@"%@%@", obj, cardType2]];
}

}];

NSLog(@"%@", finalArray);

关于ios - 在 Objective C 中连接整数数组中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22417993/

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