RunLoop与NSTimer的经典面试题

 1 #import "ViewController.h"
 2 
 3 @interface ViewController ()
 4 
 5 @property(strong,nonatomic) NSTimer *timer;
 6 
 7 @end
 8 
 9 @implementation ViewController
10 
11 - (void)viewDidLoad {
12     [super viewDidLoad];
13     
14     self.timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(runTimer) userInfo:nil repeats:YES];
15     
16     [self.timer fire];
17 }
18 
19 - (void)runTimer{
20     [NSThread detachNewThreadSelector:@selector(startTimer) toTarget:self withObject:nil];
21 }
22 
23 - (void)startTimer{
24     NSLog(@"开始执行");
25 }
26 
27 @end
28 
29 
30 /*
31 请问:“开始执行”会执行几次?
32 解答:一次。
33 原因:startTimer了在子线程上,但子线程的运动循环未开启,fire方法使用定时器强制执行一次,所以打印一次。
34 */
原文地址:https://www.cnblogs.com/panda1024/p/6278274.html