《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 ...
随机推荐
- forword/ sendRediect
res.sendRedirect(),是重定向,相当于两次请求,两次相应,地址栏会发生变化. 在实际使用中,重定向不能传指.也就是在requset中储存的值在跳转到另外一个页面后,在目标页面提取不出来 ...
- windows下clang的安装与使用
我本意是想在windows下学习下C++11,而结果是我的Visual Studio 2012不完全支持,而我又懒得去安装2013/2015,太大了.公司运维也不允许我去下载- -,然后就想能不能在w ...
- LPC18xx LPC43xx LPC4370 Bootrom USB DFU FPB - Flash Patch and Breakpoint Unit
What is the difference between a Bootrom vs bootloader on ARM systems Bootrom Bootrom (or Boot ROM) ...
- Revit如何修改云线批注外观
Revit云线批注属于注释族类别,有两种方式可以修改云线批注的外观,有两处设置可以修改云线批注的颜色线宽等外观,一个是视图属性"可见性/图形替换"对话框,另一个是菜单"管 ...
- android自适应屏幕方向和大小
一:不同的layout Android手机 屏幕 大小不一,有480x320, 640x360, 800x480.怎样才能让App自动 适应不同的屏幕 呢? 其实很简单,只需要在res目录下 ...
- Codeforces Round #379 (Div. 2) C. Anton and Making Potions 枚举+二分
C. Anton and Making Potions 题目连接: http://codeforces.com/contest/734/problem/C Description Anton is p ...
- 使用nginx-http-concat优化网站响应
前言: 我们在访问淘宝的时候,会看到代码中的js和css文件是通过一次请求或得的,我们知道浏览器一次请求只能并发访问数个资源,这样的处理错输在网络传输层面可以大大节省时间,这里使用的技术就是把css. ...
- THinkphp开启静态(动态)缓存的用法
<?php return array( //开启静态缓存 'HTML_CACHE_ON' => true, 'HTML_CACHE_RULES' => array( 'News:in ...
- C#怎样去掉对于用Splict分隔的数组中的空值?
string[] arrayUserId = userIds.Split(new char[] { ',' },StringSplitOptions.RemoveEmptyEntries); 可以去掉 ...
- 关于SpeedButton中使用下划线快捷键不响应的问题
在Windows应用程序,很多都有快捷键功能,这个Delphi也有,就是一个按钮上面有一个比如剪切(&X),这个时候剪切的快捷键就是Alt+X,这个功能有时候还是挺好用的,最近,公司中有同事, ...