Objective-C中的callback之一

本文通过一个Objective-C(NSObject)的实例演示了如何使用NSTimer与NSRunLoop来实现每隔两秒更新并打印时间的功能。该示例还展示了NSDateFormatter的单例模式使用方法。

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 2018313 下午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 2018313 下午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 2018313 下午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 2018313 下午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 2018313 下午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 2018313 下午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 2018313 下午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 2018313 下午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 2018313 下午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 2018313 下午11:07:06-

-------------------------------持续每2秒输出一次

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值