惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

K
Kaspersky official blog
Martin Fowler
Martin Fowler
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
V
Visual Studio Blog
博客园_首页
Engineering at Meta
Engineering at Meta
The Cloudflare Blog
MongoDB | Blog
MongoDB | Blog
Blog — PlanetScale
Blog — PlanetScale
T
The Blog of Author Tim Ferriss
雷峰网
雷峰网
D
Docker
博客园 - 司徒正美
S
SegmentFault 最新的问题
M
MIT News - Artificial intelligence
博客园 - 叶小钗
博客园 - 三生石上(FineUI控件)
U
Unit 42
J
Java Code Geeks
A
About on SuperTechFans
N
Netflix TechBlog - Medium
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
Security Affairs
I
Intezer
Cisco Talos Blog
Cisco Talos Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
B
Blog RSS Feed
P
Privacy & Cybersecurity Law Blog
T
Tenable Blog
T
Threatpost
H
Hacker News: Front Page
G
Google Developers Blog
博客园 - 【当耐特】
Hugging Face - Blog
Hugging Face - Blog
Apple Machine Learning Research
Apple Machine Learning Research
L
Lohrmann on Cybersecurity
大猫的无限游戏
大猫的无限游戏
Google DeepMind News
Google DeepMind News
A
Arctic Wolf
S
Secure Thoughts
GbyAI
GbyAI
NISL@THU
NISL@THU
S
Security @ Cisco Blogs
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Webroot Blog
Webroot Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
O
OpenAI News
Spread Privacy
Spread Privacy
Application and Cybersecurity Blog
Application and Cybersecurity Blog

博客园 - 墨墨

Windows Phone 7中配置gmail, 使用outlook 同步邮件,日历,联系人等 iPhone屏幕旋转的例子 objective-c的TextFields输入完成后关闭键盘和触摸背景关闭键盘 第一个iPhone程序之Hello World 第一个objective-c程序 iPhone自动化测试 visual studio 2010 ultimate 下载 suse下设置IP的两种方法 freebsd下之简单安装python ibatis.net私人学习资料,勿下(加密) ibatisnet高级查询 Python相关中文学习资料 搭建python的web开发环境 windows下 使用Aptana搭建Python开发环境 使用SharpZipLib 解压zip 2012年真的是世界末日吗?_全球关注 关于生成静态页--终极解决方案 Json格式类的转换相关代码 Extjs 登录界面源码
iPhone 闭包应用
墨墨 · 2011-07-20 · via 博客园 - 墨墨

这里的闭包自然是计算机语言中的闭包,词法闭包(Lexical Closure)的简称,主要表现在函数可以引用到自由变量,并且可以脱离变量的创建环境,再复杂的定义也不需要多说明了,可以理解为面向对象里面的成员函数可以调用其成员变量,然其概念却比面向对象的概念流行早的多,约六十年代在Scheme里就实现了。函数式编程语言里这是非常常见的概念,而非函数式的语言中js里也有很多闭包出现的身影。

准确地说在Objective-c里不叫闭包(Closure)而叫Block,但概念还是大同小异的。Mac平台上需要LLVM的支持,总之较新的Xcode都是支持这个特性的。 自由变量在使用是需要用__block关键字定义,而闭包函数的形式与函数指针非常接近,简单地说把函数指针的*换成^就可以了

 
 
typedef int (^IntBlock)();
 
IntBlock downCounter(int start)
 
{<br>&nbsp;&nbsp;&nbsp; __block int i = start; return Block_copy( ^int() { return i--; }); <br>}<br><br>
 
IntBlock f = downCounter(5); printf("%d", f()); printf("%d", f()); printf("%d", f()); Block_release(f);

就是这么简单。

单纯就语法而言,闭包并没什么好谈的,但它的应用已经越来越广,特别是iPhone新的GCD特性,广泛使用了闭包,如果不知到闭包怎么写,就会陷入明明系统提供了接口却无法调用的尴尬境地。 可能会觉得,出了系统接口必须要用block而不得不使用的情况外,其他时候直接用函数指针不就得了,实际上除非你真的用到了自由变量,其他时候 block和函数指针确实没什么区别,但block也有自己的独到之处。 首先它不需要事先定义函数,如果是函数指针那么必须要把指针指向一个函数,而函数必须要定义,闭包则不然,直接对代码块就可以调用 其次就算已经事先定义好了函数,只要对函数名取一下^即可直接放到闭包的地方使用,可以理解为向下兼容吧。 比如iPhone的动画效果,你必须先开始动画,然后写你想要动画的操作,最后提交,而开始动画和提交之间的鸿沟没法很好的协调,相当于这样

[self beginAnimation];
[self animationAction];
[self endAnimation];

而且每个动画都必需事先其专门的animationAction而iPhone的动画效果实在是个很平常的操作,这就多出了无数冗余函数,即使它只是一行hidden之类,而且也不方便扩展,闭包就很适合这种需求

 + (void)animation:(NSTimeInterval)duration withEvent:(animationEvent)event
{
[UIView beginAnimations:@"animationID" context:nil];
[UIView setAnimationDuration:duration];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
[UIView setAnimationRepeatAutoreverses:NO];
event();
[UIView commitAnimations];
 
}
 
[Tools animation:0.8 withAnimationCurve:UIViewAnimationCurveEaseInOut withEvent:
^{
[UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight
forView:centerView_ cache:YES];
// 移除功能列表
[menuView_ removeFromSuperview];
// 增加二次拔号键盘
[centerView_ addSubview:phonePad_ ];
}];