在Cocoa中组合多个字符串

时间:2014-01-23 05:35:34

标签: ios objective-c iphone nsarray stringwithformat

我希望能够将每个索引结合起来,以便获得@“Biology Teacher A BK 1”,但到目前为止我一直没有成功。这是我到目前为止所做的,但我不知道从哪里开始。

@interface ListTableViewController () <UISearchDisplayDelegate>

@property (strong, nonatomic) NSArray *className;
@property (strong, nonatomic) NSArray *teacherName;
@property (strong, nonatomic) NSArray *blockNumber;

@end

@implementation ListTableViewController

- (void)viewDidLoad {

    [super viewDidLoad];
    self.className = [NSArray arrayWithObjects:
                  @"Biology",
                  @"English III",
                  @"Chemistry",
                  @"Algebra II",
                  @"Morality", nil];

self.teacherName = [NSArray arrayWithObjects:
                    @"Teacher A",
                    @"Teacher B",
                    @"Teacher C",
                    @"Teacher D",
                    @"Teacher E", nil];

self.blockNumber = [NSArray arrayWithObjects:
                    @"BK 1",
                    @"BK 3",
                    @"BK 6",
                    @"BK 2",
                    @"BK 1", nil];
}

6 个答案:

答案 0 :(得分:1)

它会起作用:

   for (int i = 0 ; i< self.className.count; i++)
    {
      NSString *temStr = [NSString stringWithFormat:@"%@ %@ %@",[self.className objectAtIndex:i] ,[self.teacherName objectAtIndex:i],[self.blockNumber objectAtIndex:i] ];
      NSLog("%@", tempStr);
    }

答案 1 :(得分:0)

尝试使用以下代码:

for (int i = 0 ; i< self.className.count; i++)
{
  NSString *temStr = [[NSString stringWithFormat:@"%@ ", [self.className objectAtIndex:i]] stringByAppendingString:[NSString stringWithFormat:@"%@ ", [self.className objectAtIndex:i]]]
  NSLog("%@", [temStr stringByAppendingString:[self.blockNumber objectAtIndex:i]])
}

答案 2 :(得分:0)

试试这个......

int total = self.className.count;
NSMutableArray *combinedName = [NSMutableArray array];
if (total == self.teacherName.count && total == self.blockNumber.count)
{
   for(int i=0;i< total;i++)
    {
        NSString *str =[NSString stringWithFormat:@"%@ %@ %@", [self.className objectAtIndex:i],[self.teacherName objectAtIndex:i],[self. blockNumber objectAtIndex:i]];
        [combinedName addObject:str];
   }
}
else 
   NSLog(@"Cann't combine");

答案 3 :(得分:0)

试试这个

 //Assuming three array are in same length

 NSMutableArray *combineArray=[[NSMutableArray alloc] init];
 for(int i=0; i<[[self className] count]; i++)
 {
      [combineArray addObject:[NSString stringWithFormat:@"%@ %@ %@", [[self className] objectAtIndex:i],[[self teacherName] objectAtIndex:i], [[self blockNumber] objectAtIndex:i]];
 } 

 NSLog(@"%@", combineArray); //here is your output.

答案 4 :(得分:0)

你可以试试这个:

NSMutableArray *combinedArray = [[NSMutableArray alloc]init];
for (int i = 0; i < [self.className count]; i++)
{
    NSString *combinedString = [NSString stringWithFormat:@"%@ %@ %@",[self.className objectAtIndex:i],[self.teacherName objectAtIndex:i],[self. blockNumber objectAtIndex:i]];
    [combinedArray addObject:combinedString];
}
NSLog(@"Combined array is :\n %@",combinedArray);

答案 5 :(得分:0)

这样的东西会起作用,但相当难看。

self.className = [NSArray arrayWithObjects:@"Biology",@"English III",@"Chemistry",@"Algebra II",@"Morality", nil];
self.teacherName = [NSArray arrayWithObjects:@"Teacher A",@"Teacher B",@"Teacher C",@"Teacher D",@"Teacher E", nil];
self.blockNumber = [NSArray arrayWithObjects:@"BK 1",@"BK 3",@"BK 6",@"BK 2",@"BK 1", nil];

NSMutableArray *combinedNames = [[NSMutableArray alloc] init];
if (([self.className count] == [self.teacherName count]) && [self.className count] == [self.blockNumber count]) {
    for (int index = 0; index < [self.className count]; index++) {
        [combinedNames addObject:[NSString stringWithFormat:@"%@ %@ %@", [self.className objectAtIndex:index], [self.teacherName objectAtIndex:index], [self.blockNumber objectAtIndex:index]]];
    }
}

for (NSString *string in combinedNames) {
    NSLog(@"%@", string);
}

和那些输出:

Biology Teacher A BK 1
English III Teacher B BK 3
Chemistry Teacher C BK 6
Algebra II Teacher D BK 2
Morality Teacher E BK 1

<强>更新

在我完成它之前看起来这已经被其他人发布了。我没有看到他们验证数组的长度都是相同的。你可以使用anyones答案;在尝试迭代它们之前验证所有数组包含相同数量的对象可能是明智的。