2012-03-15 101 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]; 
}