zoukankan      html  css  js  c++  java
  • 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 */
  • 相关阅读:
    socket
    netstat
    列表
    突然发现不会写代码了
    算法资源
    bit位操作
    排序算法
    连续子数组最大和
    books
    凸优化
  • 原文地址:https://www.cnblogs.com/panda1024/p/6278274.html
Copyright © 2011-2022 走看看