我希望在int类型的数组中的每个对象的末尾添加一个字符串。
_finalQuestionArray是8位整数数组[2,3,4,1,2,3,4,1]
_card1Type是一个字符串@“o”
_card1Type是一个字符串@“p”
我想创建一个新的字符串数组,看起来像[2o,3o,4o,1o,2p,3p,4p,1p],但我被卡住了。如何将字符串连接到数组中的int?
到目前为止,这是我的代码: //此函数在此处附加与卡片类型对应的字母
for (int i=0; i<[_finalQuestionArray count]; i++){
if(i<([_finalQuestionArray count]/2)){
[[_finalQuestionArray objectAtIndex:i]stringByAppendingString:_card1Type];
} else {
[[_finalQuestionArray objectAtIndex:i]stringByAppendingString:_card2Type];
}
}
答案 0 :(得分:2)
这是你在找什么?
如果您通过示例学习,请查看以下内容,
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);
答案 1 :(得分:0)
他想要一个字符串,而不是数组:
NSMutableArray *_finalQuestionArray =[NSMutableArray arrayWithArray:@[@2, @3, @4, @1, @2, @3, @4, @1]];
NSString *_card1Type = @"o";
NSString *_card2Type = @"p";
NSString *finalString = @"[";
for (int i=0; i<[_finalQuestionArray count]; i++){
if(i<([_finalQuestionArray count]/2)){
finalString = [finalString stringByAppendingString:[NSString stringWithFormat:@"%@%@", _finalQuestionArray[i], _card1Type]];
} else {
finalString = [finalString stringByAppendingString:[NSString stringWithFormat:@"%@%@", _finalQuestionArray[i], _card2Type]];
}
finalString = (i == [_finalQuestionArray count]- 1) ?[finalString stringByAppendingString:@"]"] : [finalString stringByAppendingString:@", "];
}