2012-07-25 77 views
0

我正在嘗試顯示一個數組,其中包含數字(「prime」)的不同因子。但不是給我的int數,我總是得到0,1,2,3,4,5,...。在xcode中顯示數組

 factors.text = @""; 

     int factorsNumber; 

     NSMutableArray *array; 
     array = [NSMutableArray arrayWithCapacity:5]; 

     for (factorsNumber=1; factorsNumber<=prime; factorsNumber++) { 
      if (prime%factorsNumber == 0) { 
       [array addObject:[NSString stringWithFormat:@"%d", factorsNumber]]; 
      } 

     } 


     for (int i = 0; i < [array count]; i++) { 

      [array replaceObjectAtIndex:1 withObject:@"4"]; 

      NSString *temp = [NSString stringWithFormat:@"%d, ", i, [[array objectAtIndex:i] intValue]]; 
      factors.text = [factors.text stringByAppendingString:temp]; 
     } 
+0

什麼類型是'總理'? – Weston 2012-07-25 13:11:11

+0

它是一個整數 – 2012-07-25 13:12:02

回答

1

更換

NSString *temp = [NSString stringWithFormat:@"%d, ", i, [[array objectAtIndex:i] intValue]]; 

NSString *temp = [NSString stringWithFormat:@"%d, ", [[array objectAtIndex:i] intValue]]; 

的問題是,你只打印數組的索引,而不是價值。

+0

抱歉,愚蠢的問題根本沒有注意到! – 2012-07-25 13:14:46

+2

沒問題,有時它有助於有新鮮的眼睛。 – Weston 2012-07-25 13:15:24