简体   繁体   中英

Combining multiple strings in Cocoa

I want to be able to combine each index so that I get @"Biology Teacher A BK 1", but so far I have been unsuccessful. This is what I have so far, but I do not know where to go from here.

@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];
}

It Will work:

   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);
    }

Try with following code:

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]])
}

Try this...

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");

Try this

 //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.

You can try this:

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);

Something like this would work, fairly ugly though.

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);
}

and that outputs:

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

Update

Looks like this was posted already by others before I could finish getting it put together. I don't see that they verify that the array's are all of the same length though. You can use anyones answer; it might be wise to verify that all of the array's contain the same number of objects prior to trying to iterate through them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM