NStime计时器
//
// 计时器.m
// OC知识点整理
//
// Created by li on 17/2/27.
// Copyright © 2017年 李文强. All rights reserved.
//
#import "计时器.h"
@interface ___ ()
@property(weak,nonatomic)NSTimer*time;
@end
@implementation ___
/*
创建一个NStime计时器必须手动停止
*/
-(void)time{
//利用NStime每个一段时间做某事
self.time=[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerFired) userInfo:nil repeats:YES];
}
-(void)timerFired{
static int hour=0;
static int minute=0;
static int second=0;
second+=1;
if (second==60) {
minute+=1;
second=0;
if (minute==60) {
hour+=1;
minute=0;
if (hour==24) {
hour=0;
}
}
}
}
-(void)dealloc{
[self.time invalidate];
}
@end
注:使用NStime时应注意内存释放问题,防止内存泄漏