2014-01-23 41 views
0

我希望能够结合每个索引,以便得到@“生物教师BK 1”,但到目前为止,我一直不成功。这是迄今为止我所知道的,但我不知道该从哪里出发。在Cocoa中结合多个字符串

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

我没有看到你试图连接字符串的位置。请显示该代码。 – user1118321

+0

去键和价值使用字典,你会得到解决方案.. – kagmanoj

+0

@ user1118321你可以使用的代码,我写在答案... –

回答

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

如何在这里添加他想要的空间?(@“生物老师A BK 1”)# – Mani

+0

@Mani - Yup感谢您对我的正确评价。现在请检查它。 :) – iPatel

+0

这工作很好,谢谢你教给我。 – user3190962

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

谢谢你的帮助。这教会了我如何从循环中获取信息。 – user3190962

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. 
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); 
+1

这是塔帕斯朋友的答案 – Joshua

+0

@Joshua这两个答案都是相似的,这是巧合。 但它并不意味着它的副本。 顺便说一句,这个问题并不复杂的回答。 :) –

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

更新

貌似这个被别人已经发布之前,我可以完成得到它放在一起。我没有看到他们验证数组的长度都是相同的。你可以使用任何人的答案;在尝试遍历它们之前验证所有数组的对象是否包含相同数量的对象可能是明智的。