我为我正在开发的应用程序创建了一个时钟,该时钟开始正常(每次计数一秒),然后每两秒冻结一次,然后每四秒,然后八秒,等等,直到它达到 20 并且应用程序崩溃,在 xcode 显示中没有错误,甚至没有Recieved Memory Warning
.
我去了 Instruments,发现当使用我的时钟时,我的应用程序的内存正在以惊人的速度增加(尽管任何速度对我来说都是令人震惊的)。问题是,我无法弄清楚究竟是什么导致内存如此之高。该应用程序在 ARC 上运行,所以我知道我没有忘记发布一些东西。我唯一能想到的是,每次加载数字时,它都会存储到内存中并且永远不会释放。那是怎么回事,我不知道,如何解决它我什至没有想法。请让我知道我能做什么。这是我的代码:
-(void)updateTime {
calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
updateTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTime) userInfo:nil repeats:YES];
currentTime = [NSDate date];
dateComponents = [calendar components:unitFlags fromDate:currentTime];
year = [dateComponents year];
month = [dateComponents month];
day = [dateComponents day];
hour = [dateComponents hour];
minute = [dateComponents minute];
second = [dateComponents second];
if (hour >= 12) {
lblAMPM.text = @"PM";
} else {
lblAMPM.text = @"AM";
}
//if (isTwelveHour == YES) {
//make a 12 hrs clock instead of 24
if (hour >= 12) {
hour = 12 - (24-hour);
}
//}
//if midnight, show hour as 12 instead of 0
if (hour == 0) {
hour = 12;
}
if (second < 10) {
seconds = [NSString stringWithFormat:@"0%i", second];
} else {
seconds = [NSString stringWithFormat:@"%i", second];
}
if (minute < 10) {
lblTime.text = [NSString stringWithFormat:@"%i:0%i:%@", hour, minute, seconds];
} else {
lblTime.text = [NSString stringWithFormat:@"%i:%i:%@", hour, minute, seconds];
}
[lblDate setText:[NSString stringWithFormat:@"%d/%d/%d", month, day, year]];
}
- (void) updateDateFirst:(int)firstComponent setSecond:(int)secondComponent{
calendar = [NSCalendar currentCalendar];
unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;
updateTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTime) userInfo:nil repeats:YES];
currentTime = [NSDate date];
dateComponents = [calendar components:unitFlags fromDate:currentTime];
year = [dateComponents year];
month = [dateComponents month];
day = [dateComponents day];
[lblDate setText:[NSString stringWithFormat:@"%d/%d/%d", firstComponent, secondComponent, year]];
}
- (void)viewDidUnload {
lblTime = nil;
lblAMPM = nil;
lblDate = nil;
[super viewDidUnload];
}
我打电话updateTimer
来viewDidAppear
,仅供参考。