《Programming with Objective-C》第八章 Working with Blocks
Blocks are Objective-C objects, which means they can be added to collections like NSArray or NSDictionary.
Block语法——无参数版本
定义(Block的值)
^{
NSLog(@"This is a block");
}
声明
void (^simpleBlock)(void);
类似int i;
赋值
simpleBlock = ^{
NSLog(@"This is a block");
}
类似i = 2;
声明的时候定义
void (^simpleBlock)(void) = ^{
NSLog(@"This is a block");
}
类似int i = 2;
调用
simpleBlock();
Block语法——带参数版本
定义
^(double firstValue, double secondValue)
{
return firstValue * secondValue;
}
or
^double (double firstValue, double secondValue)
{
return firstValue * secondValue;
}
声明
double (^multiplyTwoValues)(double, double);
赋值
multiplyTwoValues = ^(double firstValue, double secondValue)
{
return firstValue * secondValue;
};
声明的时候定义
double (^multiplyTwoValues)(double, double) =
^(double firstValue, double secondValue)
{
return firstValue * secondValue;
};
调用
double result = multiplyTwoValues(,);
__block修饰符
int anInteger = ;
void (^testBlock)(void) = ^{ //此时只是Block定义,并没有执行里面的函数
NSLog(@"Integer is: %i", anInteger);
};
anInteger = ;
testBlock(); //Block调用 输出42
Value is captured when the block is defined.
Block定义的时候,将值复制一份给自己,所以该值已经不受外界影响。
__block int anInteger = ;
void (^testBlock)(void) = ^{ //此时只是Block定义,并没有执行里面的函数
NSLog(@"Integer is: %i", anInteger);
};
anInteger = ;
testBlock(); //Block调用 输出84
Because anInteger is declared as a __block variable, its storage is shared with the block declaration.
此时Block里面的值与外面的值共享同一份内存
Block与self的恩怨情仇
It’s important to take care when capturing self because it’s easy to create a strong reference cycle when capturing self.
因为变量默认是__strong修饰(详见这里),所以要时刻注意在block里面对self的引用(只要出现了self关键字就算引用了,因为block会自动capture)
假如self里面定义了一个block,那么self有一个指向block的strong指针(比如该block是self的一个strong成员变量);假如block里面使用了self,则block也默认拷贝了一个指向self的strong指针,此时形成strong reference cycle.
解决方法:在Block前面创建一个__weak类型的指向self的指针,并在block里面使用该指针。
例子
__weak typeof(self) weakSelf = self; //学习下这种写法哦 typeof(self)
self.simpleBlock = ^{
[weakSelf f];
};
...
self.simpleBlock();
但是,假如Block里面又有一个Block,怎么办?最好是强引用weakSelf,此时strongSelf强引用的是weakSelf而不是self,所以不会形成strong reference cycle
__weak typeof(self) weakSelf = self; //学习下这种写法哦 typeof(self)
self.simpleBlock = ^{
[weakSelf f];
__strong typeof(weakSelf) strongSelf = weakSelf;
self.simpleBlock2 = ^{
[strongSelf f];
};
self.simpleBlock2();
};
...
self.simpleBlock();
图解

一个函数最好只有一个Block参数,且最好是在最后一个
A Block Should Always Be the Last Argument to a Method.
It’s best practice to use only one block argument to a method.
使用typedef定义一个block
typedef int (^Sum)(int, int);
Sum mySum = ^(int a, int b){
return a+b;
}
or
typedef void (^XYZSimpleBlock)(void);
@property (copy) XYZSimpleBlock blockProperty;
使用copy修饰block的property
@property (nonatomic, copy) Sum mySum;
@property (nonatomic, copy) void (^blockProperty)(void);
非ARC下就必须写copy,because a block needs to be copied to keep track of its captured state outside of the original scope
在ARC下写不写copy都无所谓,so it's a best practice to set copy property for block whether it's ARC or not.
《Programming with Objective-C》第八章 Working with Blocks的更多相关文章
- Programming In Scala笔记-第八章、函数与闭包
当程序的代码量增大时,就需要对各功能模块进行分割,这些分割的小模块就是本文中接下来会进行分析的函数.接下来的部分会讲解包括函数嵌套,函数字面量,以及函数值等概念. 一.方法 一会函数一会方法的,是不是 ...
- 《Programming with Objective-C》
苹果官方文档:不稳定的传送门 读书笔记共有以下几篇,其他的知识点不重要或者已经熟悉不需记录 <Programming with Objective-C>第三章 Working with O ...
- Automake
Automake是用来根据Makefile.am生成Makefile.in的工具 标准Makefile目标 'make all' Build programs, libraries, document ...
- Oleg Sych - » Pros and Cons of T4 in Visual Studio 2008
Oleg Sych - » Pros and Cons of T4 in Visual Studio 2008 Pros and Cons of T4 in Visual Studio 2008 Po ...
- Pros and Cons of T4 in Visual Studio 2008
Oleg Sych - » Pros and Cons of T4 in Visual Studio 2008 Pros and Cons of T4 in Visual Studio 2008 Po ...
- Configuring a remote m-phy
An interface for low power, high bandwidth communications between units in a device in provided here ...
- 2018-11-27 中文代码示例之Programming in Scala笔记第七八章
续前文: 中文代码示例之Programming in Scala学习笔记第二三章 中文代码示例之Programming in Scala笔记第四五六章. 同样仅节选有意思的例程部分作演示之用. 源文档 ...
- Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第十八章:立方体贴图
原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第十八章:立方体贴图 代码工程地址: https://github.c ...
- Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第八章:光照
原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第八章:光照 代码工程地址: https://github.com/j ...
随机推荐
- paip.网页右键复制菜单限制解除解决方案
paip.网页右键复制菜单限制解除解决方案 作者Attilax 艾龙, EMAIL:1466519819@qq.com 来源:attilax的专栏 地址:http://blog.csdn.net ...
- Tomcat之web项目部署
Tomcat一般用于部署JavaWeb项目. 遇到的问题 Linux操作系统中,在tomcat中部署项目时,一般只需要把项目war包:demo.war放到webapps下,然后启动tomcat即可.这 ...
- HTML5之语义化标签
HTML 5的革新之一:语义化标签一节元素标签. 在HTML 5出来之前,我们用div来表示页面章节,但是这些div都没有实际意义.(即使我们用css样式的id和class形容这块内容的意义).这些标 ...
- INNO setup安装卸载钱判断进程中是否在运行总结
1.安装前判断进程中是否有程序在运行. [files] ; 安装前判断进程,dll文件放在inno的安装目录中Source: compiler:psvince.dll; Flags: dontcopy ...
- 【简单易懂的AMV图文教程-2】VEGAS基础进阶——认识关键帧
[简单易懂的AMV图文教程-2]VEGAS基础进阶--认识关键帧 经过了上一期VEGAS基础教程的学习,相信大家都能独立完成一些比较简单的纯剪辑作品了.今天在这里为大家继续介绍VEGAS的一大基础应用 ...
- [MS bug]安装SQL Server 2008 错误:is not a valid login or you do not have permission
环境: Windows 7 sp1 x64. 问题描述: 安装到几乎要完成的时候爆出:is not a valid login or you do not have permission.安装失败. ...
- PreparedStatement ResultSet
public int searchProblemDistinctCount() throws Exception { DBOperator dbo = getDBOperator(); try { P ...
- 2013eoe移动开发者大会圆满落幕
(国内知名Android开发论坛.安卓开发社区推荐:http://www.eoeandroid.com/) 2013eoe移动开发者大会9月14号于国家会议中心盛大召开并圆满结束,超过2000个开发者 ...
- 如何做一份能忽悠投资人的PPT
游侠近日发布的一款电动汽车引发全民吐槽,被人们嘲讽为“靠一份PPT忽悠投资人”.这类情形可以回溯至去年的锤子手机发布会.如今,吐槽的开始散去,我们可以静下心来吸收点干货,我们对比了锤子手机发布会的PP ...
- MAC 磁盘清理工具 ncdu
下载命令:brew install ncdu 使用命令:ncdu . 它会将当前目录下的所有文件.文件夹大小安倒叙排列,方便清除