zoukankan      html  css  js  c++  java
  • iOS开发—block介绍

    - (void)viewDidLoad {
        [super viewDidLoad];
        NSLog(@"我在玩手机");
        NSLog(@"手机没电了");
        [self chargeMyIphone:^{
            NSLog(@"出门逛街");
        }];
        NSLog(@"我在看电视");
    }
    
    -(void)chargeMyIphone:(void(^)(void))finishBlock
    {
        double delayInSeconds = 10.0;
        dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
        dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
            NSLog(@"电充好了");
            finishBlock();
        });
    }
    

    (void(^)(void))finishBlock 第一个void表示此block无返回值。(^)为block type的标志。第二个(void)表示这个block无参数。finishBlock就是他的名字。无参数无返回类型的匿名函数就是我们的最简单的block了!他非常方便我们用来回调,因为他没有返回值,没有参数,就相当于只有内部的可执行代码!

    performSelector: 和dispatch_time  是iOS中的多线程

    参考:http://blog.csdn.net/mobanchengshuang/article/details/11751671

  • 相关阅读:
    【poj3764】 The xor-longest Path
    【poj3261】 Milk Patterns
    【poj3237】 Tree
    【bzoj2654】 tree
    【poj3122】 Pie
    【poj1011】 Sticks
    【poj1186】 方程的解数
    【poj2741】 Colored Cubes
    【poj3141】 Distant Galaxy
    【bzoj2456】 mode
  • 原文地址:https://www.cnblogs.com/saurik/p/4940142.html
Copyright © 2011-2022 走看看