Reputation: 31
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];
}
Upvotes: 0
Views: 95
Reputation: 7414
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.
Upvotes: 0
Reputation: 47069
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]])
}
Upvotes: 0
Reputation: 2555
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);
}
Upvotes: 1
Reputation: 280
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);
Upvotes: 0
Reputation: 7207
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.
Upvotes: 0
Reputation: 17585
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");
Upvotes: 0