-3
我在我的秒錶應用程序中有此代碼,但是我有循環問題,我的數組中的所有值都是相同的。我想把時間觀察一個接一個地排列。進行觀察,添加到陣列並採取新的觀察並繼續進行,直到樣本大小爲t。我如何使用循環,非常感謝幫助。在iOS中使用循環
- (IBAction)onLapPressed:(id)sender {
timeData = [[NSMutableArray alloc] init];
for (NSInteger k = 0; k < t; ++k)
{
[timeData addObject:[NSNull null]];
}
NSDate *currentDate = [NSDate date];
NSTimeInterval timeInterval = [currentDate timeIntervalSinceDate:startDate];
NSDate *timerDate = [NSDate dateWithTimeIntervalSince1970:timeInterval];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"ss.S"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0.0]];
NSString *timeString=[dateFormatter stringFromDate:timerDate];
CGFloat timeStringFloat = (CGFloat) [timeString floatValue];
[timeData replaceObjectAtIndex:i withObject:timeString];
lap1.text = [timeData objectAtIndex:i];
NSString *iyaz = [NSString stringWithFormat: @"%d",i+1];
NSString *tyaz = [NSString stringWithFormat: @"%d",t];
NSString *baseString = [NSString stringWithFormat: @"%@/%@",iyaz,tyaz];
lap2.text = baseString;
totaltime = totaltime + timeStringFloat;
x2 = x2 + powf(timeStringFloat, 2);
n = round((40* sqrtf(t*x2 - powf(totaltime, 2))/totaltime));
avgtime = totaltime/(i+1);
NSString *strtotaltime = [NSString stringWithFormat:@"%f", avgtime];
NSString *toobserve = [NSString stringWithFormat:@"%d", n];
avglap.text = strtotaltime;
obsno.text = toobserve;
startDate = [[NSDate date]retain];
i=i+1;
}
http://stackoverflow.com/questions/15076294/mutable-array-values-are-exactly-same – vikingosegundo 2013-03-20 12:10:50
的重職哪裏了't'從何而來? – holex 2013-03-20 12:11:39