2012-03-15 96 views
1

的數組,如果我有這樣的:打印答案

int array[3] = {1,2,3}; 

for (int i=0; i<3; i++) { 
    int answers = (array[i] + 1); 

    NSLog(@"%d"); 

    NSString *text = [NSString stringWithFormat:@"%d ",answers]; 

    self.label.text = text; 
} 

該聲明的NSLog打印出所有的答案。但標籤只顯示最後一個值。 :( 如何使標籤顯示所有三個答案?

謝謝。

+2

+1傢伙請不要勸阻新手!給他們一些時間來學習! – EmptyStack 2012-03-15 06:47:28

+0

不是應該的NSLog(@「%d」,答案)? – chikuba 2012-03-15 06:51:38

回答

0

只會打印的最後一個值的原因是,它會覆蓋前一個。把延遲看到其他值太

+0

我怎麼做?我是Xcode的新手。:( – Hauwa 2012-03-15 06:18:55

+0

這與Xcode無關。 – 2012-03-15 06:34:55

0
int array[3] = {1,2,3}; 
NSMutableString *mutableStr = [[NSMutableString alloc]init]; 
for (int i=0; i<3; i++) { 
    int answers = (array[i] + 1); 
    NSLog(@"%d"); 

    NSString *text = [NSString stringWithFormat:@"%d ",answers]; 
    [mutableStr appendFormat:@"%@ ",text]; 
} 

self.label.text = text; 
[mutableStr release]; 
+0

也許使字符串autorealease? – chikuba 2012-03-15 07:09:49

+0

ooops。是的,我錯過了。編輯我的答案。謝謝 :) – janusbalatbat 2012-03-15 07:16:39

1
int array[3] = {1,2,3}; 

// before you can set the label to something 
self.label.text = @"answer:"; 

for (int i=0; i<3; i++) { 
    int answers = (array[i] + 1); 

    NSLog(@"%d", answer); 

    NSString *text = [NSString stringWithFormat:@"%d ",answers]; 

    self.label.text = [self.label.text stringByAppendingString:text]; 
}