NSTimer定时器的简单用法/NSTimer Instance
2015-10-06 21:44
417 查看
NSTimer定时器的简单用法
版本信息:
OS version : 10.10
Interface Name: NSTimer
Location : Frameworks/Foundation/NSTimer.h
OC源码:
#import <Foundation/NSObject.h>
#import <Foundation/NSDate.h>
@interface NSTimer :NSObject
//类方法
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;
今天只关注上面的这个类方法,这个类方法很实用滴,(打印日志文件,纪录用户操作等等)。
以打印日志文件为例,需要用到3个文件:
Log.h
Log.m
main.m
版本信息:
OS version : 10.10
Interface Name: NSTimer
Location : Frameworks/Foundation/NSTimer.h
OC源码:
#import <Foundation/NSObject.h>
#import <Foundation/NSDate.h>
@interface NSTimer :NSObject
//类方法
+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;
今天只关注上面的这个类方法,这个类方法很实用滴,(打印日志文件,纪录用户操作等等)。
以打印日志文件为例,需要用到3个文件:
Log.h
#import <Foundation/Foundation.h> @interface Log : NSObject - (void) Log;//for log - (void) timerAction:(NSTimer *)timer; @end
Log.m
#import "Log.h" @implementation Log - (void)Log { NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *path = NSHomeDirectory(); NSString *filePath = [path stringByAppendingString:@"/IOS/Log.txt"];//create a txt file for log BOOL success = [fileManager createFileAtPath:filePath contents:nil attributes:nil]; if (success) { NSLog(@"create success"); } //NSFileHandle for handle the txt file NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:filePath]; [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerAction:) userInfo:fileHandle repeats:YES]; //NSTimer定时器使用,每一秒call一次timerAction,写入当前时间到txt file 里面 } - (void) timerAction:(NSTimer *)timer { static int i = 0; NSFileHandle *fileHandle = timer.userInfo; [fileHandle seekToEndOfFile]; NSDate *now = [NSDate date]; NSDateFormatter *dateFormate = [[NSDateFormatter alloc] init]; [dateFormate setDateFormat:@" --- yyyy/MM/dd HH:mm:ss"]; NSString *dateNowString = [dateFormate stringFromDate:now]; dateNowString = [dateNowString stringByAppendingString:@"\n"]; NSData *data = [dateNowString dataUsingEncoding:NSUTF8StringEncoding]; [fileHandle writeData:data]; if (i == 10) { [timer invalidate]; [fileHandle closeFile]; } i++; } @end
main.m
#import <Foundation/Foundation.h> #import "Log.h" int main(int argc, const char * argv[]) { @autoreleasepool { // insert code here... Log *write = [[Log alloc] init]; [write log]; } [[NSRunLoop currentRunLoop] run];//NSTImer 和 NSRunLoop 配合使用 return 0; }
相关文章推荐
- linux mknod命令解析
- stm32F103 模拟I2C mpu6050收到数据全为0,或者地址为209,104,0x68,0xD0的一些解决办法总结
- ns-2与TCP拥塞模拟 之六 ns-2 TCP拥塞控制核心模块-变量说明
- spring 作用域之单例模式/原型模型
- Java EE (8) -- Java EE Patterns
- Android Studio 之路1
- 程序在内存中占的四个区域
- OA权限模块实体映射和数据初始化
- [root@localhost zhu]# yum install vim Loaded plugins: langpacks, presto, refresh-packagekit Adding e
- point on C_5 操作符和表达式
- 2015第41周二
- Codeforces Round #323 (Div. 2)
- Extjs extjs datefield日期格式
- iOS开发------多线程编程(2)
- point on C_4 语句
- D3学习简介以及资料分享
- Git merge 合并分区详解
- 利用绝对布局制作登陆页面(改进)
- Matlab GUI设计——文件读取和保存uigetfile,uiputfile
- R连接MySQL数据库方法