2014-01-23 40 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 

更新

貌似這個被別人已經發布之前,我可以完成得到它放在一起。我沒有看到他們驗證數組的長度都是相同的。你可以使用任何人的答案;在嘗試遍歷它們之前驗證所有數組的對象是否包含相同數量的對象可能是明智的。