0

我希望能够结合每个索引,以便我得到@“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];
}
4

6 回答 6

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);
    }
于 2014-01-23T05:46:56.570 回答
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");
于 2014-01-23T05:42:48.653 回答
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.
于 2014-01-23T05:43:54.370 回答
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);
于 2014-01-23T05:46:23.403 回答
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

更新

看起来这是其他人在我完成组装之前已经发布的。我没有看到他们验证数组的长度是否相同。您可以使用任何人的答案;在尝试遍历它们之前验证所有数组是否包含相同数量的对象可能是明智的。

于 2014-01-23T05:48:09.797 回答
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]])
}
于 2014-01-23T05:42:21.927 回答