- 浏览: 461243 次
- 性别:
- 来自: 广州
文章分类
- 全部博客 (538)
- C/C++ Primer (69)
- Objective-C Primer (102)
- Python Primer (19)
- JavaScript Primer (1)
- Java Primer (37)
- PHP Primer (17)
- 泛 Linux (37)
- Shell Script (21)
- APUE (21)
- UNP__1&2 (19)
- NetWork (7)
- Oracle周边 (38)
- Mysql里边 (6)
- Windows技 (9)
- 简单算法 & 数据结构 (14)
- 设计模式 (6)
- GTK历程 (12)
- 工具使用 (25)
- 杂事 (23)
- 一些概念 (17)
- Web方面 (10)
- myCodeTools (9)
- ^未 竟$ (13)
- 硬件通信 (2)
- Games (1)
最新评论
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
样子和函数指针像死了=。=
block作为方法的参数使用,传个函数指针给人家执行:
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
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
发表评论
-
float equal
2013-05-23 18:21 822- (BOOL)floatA:(float)f1 equalB ... -
Coding Guidelines for Cocoa
2013-05-17 16:53 673参考: https://developer.apple.com ... -
Cell
2013-05-16 14:32 791- (void)tableView:(UITableView ... -
Object-C编程规范
2013-05-15 10:49 7391.参考苹果的文档 “Coding Guidelines fo ... -
MacPorts
2013-02-28 18:12 616http://blog.csdn.net/lynjay/art ... -
KVC/KVO 监听对象属性变化
2013-01-10 23:09 7404http://blog.csdn.net/a6472953/a ... -
ios Associative 扩展属性
2013-01-08 16:45 1541@dynamic和@synthesize http://blo ... -
ObjC Dynamic
2013-01-08 15:21 778原文:http://www.onevcat.com/2012/ ... -
UITableView & UITextField
2012-11-08 00:35 843keyboard event ---------------- ... -
UIWebView
2012-10-24 11:06 753http://hi.baidu.com/wei_1123/it ... -
Device orientation
2012-10-15 16:20 753- (BOOL)shouldAutorotateToInter ... -
system notification
2012-10-12 15:22 631for UIApplication These notifi ... -
NSZombies
2012-08-09 08:51 673NSZombies搞定EXC_BAD_ACCESS http: ... -
Quartz 2D Programming Guide
2012-07-17 00:51 600Quartz 2D 内容不少啊 Graphics Trans ... -
NSCache
2012-07-12 14:25 761http://thenewself.blog.163.com/ ... -
NSCoding
2012-07-12 13:39 966@protocol NSCoding - (void ... -
分析 crash 报告的方法
2012-07-12 01:15 588http://blog.csdn.net/toss156/ar ... -
Multi-touch
2012-07-11 23:33 731智能与灵活与工作量>_< http://www.o ... -
iOS的多核编程和内存管理
2012-07-11 18:14 720http://anxonli.iteye.com/blog/1 ... -
NSCopying
2012-07-10 15:54 524http://www.apple.com.cn/develop ...
相关推荐
### Blocks Programming Topics #### Introduction Blocks是C语言层面的一个语法和运行时特性,它们类似于标准C函数,但除了可执行代码之外,还可以包含对自动(栈)或管理(堆)内存中的变量绑定。这意味着一个块...
### Blocks编程要点详解 #### 简介 Block对象是一种C级别的语法和运行时特性,在iOS 4.0及以后的版本中得到了广泛的应用。它们类似于标准C函数,但不仅包含可执行代码,还能捕获并维护一组局部变量的状态。这种...
因此,Apple提供的《Blocks Programming Topics》文档是学习和实践Blocks编程的宝贵资源。通过对文档的学习,开发者可以深入理解Blocks的声明和创建过程,掌握如何在程序中有效地使用Blocks,提升编程效率和代码质量...
关于iOS开发辅助的四本书,经謝業蘭朋友翻译,供大家学习参考。 核心动画编程指南.pdf-- (Apple官方原版:Core Animation Programming ...Blocks 编程要点.pdf------ (Apple官方原版:Blocks Programming Topics.pdf)
SIMATIC PCS 7 Programming Instructions for Blocks manualpdf,SIMATIC PCS 7 Programming Instructions for Blocks manual
在Apple官方文档《Blocks Programming Topics》中,详细介绍了block编程的各种要点,包括block的声明、创建、使用、与变量的关系等方面。文档结构清晰,从基础知识到高级应用,逐步深入,适合初入门的iOS开发人员...
Programming LEGO EV3 My Blocks Teaching Concepts and Preparing for FLL Competition 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 查看此书详细信息请在美国亚马逊官网搜索此书
书名《Pro TBB C++ Parallel Programming with Threading Building Blocks》指出了本书的主要内容是使用Threading Building Blocks (TBB) 进行C++并行编程。Threading Building Blocks 是一个由Intel开发的C++模板库...
scratch-blocks, Scratch块是构建创造性计算接口的库 块Scratch块块是构建创造性计算界面的库。 简介Scratch块是 google 项目的一个 fork,它提供了一个设计规范和代码库,用于构建创新计算接口。 代码库支持快速...
在IT领域,尤其是在C/C++多线程编程中,Intel Threading Building Blocks(简称TBB)是一个备受赞誉的工具库,它为并发编程提供了一套现代、工业级的解决方案。以下将深入探讨Intel TBB的核心概念、优势以及其在多核...
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...
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 ...
Traditional Building Blocks of Concurrency Chapter 4. Asynchronous Programming with Futures and Promises Chapter 5. Data-Parallel Collections Chapter 6. Concurrent Programming with Reactive ...