我的数组的for循环目前导致同样的数字:0,1,2,3,4,5,6 .... 1000。 下面是代码:
pickerArray = [[NSMutableArray alloc] initWithCapacity:700];
for ( int i = 1 ; i <= 1000 ; i = i++)
[pickerArray addObject:[NSString stringWithFormat:@"%i", i]]
我想它代替,具有以下图案,0,2.5,5,7.5,10 CodeGo.net,12.5,15,17.5,20,等等。 而正是这样的代码:
float weight = [[pickerArray objectAtIndex:row] intValue];
label.text = [NSString stringWithFormat:@"%f lb", weight];
1.
pickerArray = [[NSMutableArray alloc] initWithCapacity:700];
for ( float i = 0.0 ; i <= 1000.0 ; i = i + 2.5)
{
//[pickerArray addObject:[NSString stringWithFormat:@"%.1f", i]]
[pickerArray addObject:[NSNumber numberWithFloat:i];
}
float weight = [[pickerArray objectAtIndex:row] floatValue];
label.text = [NSString stringWithFormat:@"%.1f", weight];
2. 您可以在回路中做到这一点:
for ( float f = 0; f <= 1000; f = f + 2.5 )
[pickerArray adObject:[NSString stringWithFormat:@"%.1f", f]];
或者你可以在创建字符串时只需乘以:
[pickerArray adObject:[NSString stringWithFormat:@"%.1f", 2.5*i]];
另外,如果你把1000个对象中,为什么700的初始容量开始?而且你真的确定你要在这个数组中所有的字符串? 编辑:和看了你的编辑,你真的确定要这么做看似冗余的来回转换为字符串?好吧,如果你确定:
float weight = [[pickerArray objectAtIndex:row] floatValue];
3. 试试这样:
pickerArray = [[NSMutableArray alloc] initWithCapacity:1001];
for ( int i = 0 ; i <= 1000 ; ++i)
[pickerArray addObject:[NSString stringWithFormat:@"%.1f", 2.5*i]]