`

Blocks Programming

 
阅读更多
https://developer.apple.com/library/ios/#featuredarticles/Short_Practical_Guide_Blocks/_index.html
https://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/Blocks/Articles/00_Introduction.html
__block的例子:http://www.cnblogs.com/bandy/archive/2012/06/05/2536973.html
理解为java的内部类、C++嵌套类。
这个作用是体现了语言的封装性,多态,回调起来更隐蔽。

开源例子:UIAlertView-Blocks,使用代码块来回调。
https://github.com/jivadevoe/UIAlertView-Blocks


样子和函数指针像死了=。=
//定义
int (^Multiply)(int, int) = ^(int num1, int num2) {
    return num1 * num2;
};
//调用
int result = Multiply(7, 4); // result is 28


    char *myCharacters[3] = { "CC", "BB", "AA" };
    
    qsort_b(myCharacters, 3, sizeof(char *), ^(const void *l, const void *r) 
    {
        char *left = *(char **)l;
        char *right = *(char **)r;
        return strncmp(left, right, 1);
    });
        
    NSLog(@"%s", myCharacters[0]);
    NSLog(@"%s", myCharacters[1]);
    NSLog(@"%s", myCharacters[2]);


block作为方法的参数使用,传个函数指针给人家执行:
-(int)testBlockVar1:(int)var1 
               Var2:(int)var2 
              block:(int(^)(int a, int b))block
{
    return block(var1,var2);
}


- (IBAction)actionTest:(id)sender {
    
    
    int k = [self testBlockVar1:11 Var2:22 block:^(int c,int d)
             {
                 return c+d;
             }];
    
    NSLog(@"k = %d", k);
}





//原来的做法
- (void)viewDidLoad {
   [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserver:self
        selector:@selector(keyboardWillShow:)
        name:UIKeyboardWillShowNotification object:nil];
}
 
- (void)keyboardWillShow:(NSNotification *)notification {
    // Notification-handling code goes here.
}

//新的做法
- (void)viewDidLoad {
    [super viewDidLoad];
    [[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillShowNotification
         object:nil queue:[NSOperationQueue mainQueue] 
usingBlock:^(NSNotification *notif) {
             // Notification-handling code goes here. 
    }];
}



Why Use Blocks?

One can discern a half-dozen or so use cases for blocks in framework methods:

Completion handlers
Notification handlers
Error handlers
Enumeration
View animation and transitions
Sorting
分享到:
评论

相关推荐

    Blocks Programming Topics

    ### Blocks Programming Topics #### Introduction Blocks是C语言层面的一个语法和运行时特性,它们类似于标准C函数,但除了可执行代码之外,还可以包含对自动(栈)或管理(堆)内存中的变量绑定。这意味着一个块...

    Blocks编程要点[中文完整翻译版]- Blocks Programming Topics

    ### Blocks编程要点详解 #### 简介 Block对象是一种C级别的语法和运行时特性,在iOS 4.0及以后的版本中得到了广泛的应用。它们类似于标准C函数,但不仅包含可执行代码,还能捕获并维护一组局部变量的状态。这种...

    Blocks变成要点

    因此,Apple提供的《Blocks Programming Topics》文档是学习和实践Blocks编程的宝贵资源。通过对文档的学习,开发者可以深入理解Blocks的声明和创建过程,掌握如何在程序中有效地使用Blocks,提升编程效率和代码质量...

    [翻译中文]4本iOS开发Apple官方原版教材-Core Animation.....

    关于iOS开发辅助的四本书,经謝業蘭朋友翻译,供大家学习参考。 核心动画编程指南.pdf-- (Apple官方原版:Core Animation Programming ...Blocks 编程要点.pdf------ (Apple官方原版:Blocks Programming Topics.pdf)

    SIMATIC PCS 7 Programming Instructions for Blocks manual.pdf

    SIMATIC PCS 7 Programming Instructions for Blocks manualpdf,SIMATIC PCS 7 Programming Instructions for Blocks manual

    iOS block编程要点

    在Apple官方文档《Blocks Programming Topics》中,详细介绍了block编程的各种要点,包括block的声明、创建、使用、与变量的关系等方面。文档结构清晰,从基础知识到高级应用,逐步深入,适合初入门的iOS开发人员...

    Programming LEGO EV3 My Blocks Teaching Concepts and Preparing for FLL epub

    Programming LEGO EV3 My Blocks Teaching Concepts and Preparing for FLL Competition 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 查看此书详细信息请在美国亚马逊官网搜索此书

    Pro TBB C++ Parallel Programming with Threading Building Blocks

    书名《Pro TBB C++ Parallel Programming with Threading Building Blocks》指出了本书的主要内容是使用Threading Building Blocks (TBB) 进行C++并行编程。Threading Building Blocks 是一个由Intel开发的C++模板库...

    scratch-blocks, Scratch块是构建创造性计算接口的库.zip

    scratch-blocks, Scratch块是构建创造性计算接口的库 块Scratch块块是构建创造性计算界面的库。 简介Scratch块是 google 项目的一个 fork,它提供了一个设计规范和代码库,用于构建创新计算接口。 代码库支持快速...

    intel thread building blocks

    在IT领域,尤其是在C/C++多线程编程中,Intel Threading Building Blocks(简称TBB)是一个备受赞誉的工具库,它为并发编程提供了一套现代、工业级的解决方案。以下将深入探讨Intel TBB的核心概念、优势以及其在多核...

    Learning Processing A Beginner's Guide to Programming Images, Animation 2nd

    It teaches the basic building blocks of programming needed to create cutting-edge graphics applications including interactive art, live video processing, and data visualization. Step-by-step examples...

    Packt.Swift.Functional.Programming.2nd.Edition.2017

    It starts with the basics of FP, and you will go through all the core concepts of Swift and the building blocks of FP. You will also go through important aspects, such as function composition and ...

    Learning Concurrent Programming in Scala, 2nd Edition

    Traditional Building Blocks of Concurrency Chapter 4. Asynchronous Programming with Futures and Promises Chapter 5. Data-Parallel Collections Chapter 6. Concurrent Programming with Reactive ...

Global site tag (gtag.js) - Google Analytics