OC中的Callback有四种类型:
下面是第一种:NSRunloop
//
// BNRLogger.h
// TOCRunloopa
//
//
#import <Foundation/Foundation.h>
@interface BNRLogger : NSObject
@property (nonatomic) NSDate *lastTime;
- (NSString *)lastTimerString;
- (void)updateLastTimer:(NSTimer *)t;
@end
//
// BNRLogger.m
// TOCRunloopa
//
//
#import "BNRLogger.h"
@implementation BNRLogger
- (NSString *)lastTimerString{
static NSDateFormatter *dateFormatter = nil;
if(!dateFormatter) {
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
NSLog(@"created dateFormatter");
}
return [dateFormatter stringFromDate:self.lastTime];
}
- (void)updateLastTimer:(NSTimer *)t {
NSDate *now = [NSDate date];
[self setLastTime:now];
NSLog(@"I want to see the timer here: ");
NSLog(@"Just set time to %@", self.lastTimerString);
}
@end
//
// main.m
// TOCRunloopa
//
//
#import <Foundation/Foundation.h>
#import "BNRLogger.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
// insert code here...
BNRLogger *logger = [[BNRLogger alloc] init];
__unused NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2.0
target:logger
selector:@selector(updateLastTimer:)
userInfo:nil
repeats:YES];
[[NSRunLoop currentRunLoop] run];
}
return 0;
}
Result:
2018-03-13 23:06:48.385109+0800 TOCRunloopa[70235:4288894] I want to see the timer here:
2018-03-13 23:06:48.385832+0800 TOCRunloopa[70235:4288894] created dateFormatter
2018-03-13 23:06:48.388740+0800 TOCRunloopa[70235:4288894] Just set time to 2018年3月13日 下午11:06:48
2018-03-13 23:06:50.389817+0800 TOCRunloopa[70235:4288894] I want to see the timer here:
2018-03-13 23:06:50.390015+0800 TOCRunloopa[70235:4288894] Just set time to 2018年3月13日 下午11:06:50
2018-03-13 23:06:52.385937+0800 TOCRunloopa[70235:4288894] I want to see the timer here:
2018-03-13 23:06:52.386101+0800 TOCRunloopa[70235:4288894] Just set time to 2018年3月13日 下午11:06:52
2018-03-13 23:06:54.389943+0800 TOCRunloopa[70235:4288894] I want to see the timer here:
2018-03-13 23:06:54.390161+0800 TOCRunloopa[70235:4288894] Just set time to 2018年3月13日 下午11:06:54
2018-03-13 23:06:56.389912+0800 TOCRunloopa[70235:4288894] I want to see the timer here:
2018-03-13 23:06:56.390110+0800 TOCRunloopa[70235:4288894] Just set time to 2018年3月13日 下午11:06:56
2018-03-13 23:06:58.385713+0800 TOCRunloopa[70235:4288894] I want to see the timer here:
2018-03-13 23:06:58.385988+0800 TOCRunloopa[70235:4288894] Just set time to 2018年3月13日 下午11:06:58
2018-03-13 23:07:00.385853+0800 TOCRunloopa[70235:4288894] I want to see the timer here:
2018-03-13 23:07:00.386030+0800 TOCRunloopa[70235:4288894] Just set time to 2018年3月13日 下午11:07:00
2018-03-13 23:07:02.386859+0800 TOCRunloopa[70235:4288894] I want to see the timer here:
2018-03-13 23:07:02.387024+0800 TOCRunloopa[70235:4288894] Just set time to 2018年3月13日 下午11:07:02
2018-03-13 23:07:04.390148+0800 TOCRunloopa[70235:4288894] I want to see the timer here:
2018-03-13 23:07:04.390356+0800 TOCRunloopa[70235:4288894] Just set time to 2018年3月13日 下午11:07:04
2018-03-13 23:07:06.390241+0800 TOCRunloopa[70235:4288894] I want to see the timer here:
2018-03-13 23:07:06.390478+0800 TOCRunloopa[70235:4288894] Just set time to 2018年3月13日 下午11:07:06-