如何连接两个数组的字符串?

时间:2011-08-25 16:53:01

标签: objective-c nsmutablearray

这就是我所拥有的

NSMutableArray Code = [COMM 112, MATH 101, SCI 201];
NSMutableArray Name = [Commerce, Calculas , Science];

NSMutableArray Course;
for(int i=0; i < [code count]; i++)
{
    [Course objectAtIndex:i] = [Code objectAtIndex:i] + "-" + [Name objectAtIndex:i];
}

我最终希望得到什么:

Course = [COMM 112 - Commerce, MATH 101 - Calculas, SCI 201 - Science];

这只是伪代码!有任何帮助实现这一目标吗?

1 个答案:

答案 0 :(得分:3)

NSMutableArray * courseCodes = [@"COMM 112", @"MATH 101", @"SCI 201"];
NSMutableArray * courseNames = [@"Commerce", @"Calculus" , @"Science"];

// Check: if ([courseCodes count] != [courseNames count]) something went wrong...   

NSMutableArray * courseDescriptions = [NSMutableArray array];
for(int i = 0; i < [courseCodes count]; i++)
{
    NSString * courseDescription = [NSString stringWithFormat:@"%@ - %@",
                                    [courseCodes objectAtIndex:i],
                                    [courseNames objectAtIndex:i]];
    [courseDescriptions addObject:courseDescription];
}