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的更多相关文章

  1. Programming In Scala笔记-第八章、函数与闭包

    当程序的代码量增大时,就需要对各功能模块进行分割,这些分割的小模块就是本文中接下来会进行分析的函数.接下来的部分会讲解包括函数嵌套,函数字面量,以及函数值等概念. 一.方法 一会函数一会方法的,是不是 ...

  2. 《Programming with Objective-C》

    苹果官方文档:不稳定的传送门 读书笔记共有以下几篇,其他的知识点不重要或者已经熟悉不需记录 <Programming with Objective-C>第三章 Working with O ...

  3. Automake

    Automake是用来根据Makefile.am生成Makefile.in的工具 标准Makefile目标 'make all' Build programs, libraries, document ...

  4. 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 ...

  5. 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 ...

  6. Configuring a remote m-phy

    An interface for low power, high bandwidth communications between units in a device in provided here ...

  7. 2018-11-27 中文代码示例之Programming in Scala笔记第七八章

    续前文: 中文代码示例之Programming in Scala学习笔记第二三章 中文代码示例之Programming in Scala笔记第四五六章. 同样仅节选有意思的例程部分作演示之用. 源文档 ...

  8. Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第十八章:立方体贴图

    原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第十八章:立方体贴图 代码工程地址: https://github.c ...

  9. Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第八章:光照

    原文:Introduction to 3D Game Programming with DirectX 12 学习笔记之 --- 第八章:光照 代码工程地址: https://github.com/j ...

随机推荐

  1. Javascript提升阶段学习

    JavaScript1:javascript简介 JavaScript是一种脚本语言,能实现网页内容的交互显示,当用户在客户端显示该网页时,浏览器就会执行JavaScript程序,用户通过交互的操作来 ...

  2. 分析Linux内核中进程的调度(时间片轮转)-《Linux内核分析》Week2作业

    1.环境的搭建: 这个可以参考孟宁老师的github:mykernel,这里不再进行赘述.主要是就是下载Linux3.9的代码,然后安装孟宁老师编写的patch,最后进行编译. 2.代码的解读 课上的 ...

  3. Leetcode 102 Binary Tree Level Order Traversal 二叉树+BFS

    二叉树的层次遍历 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * ...

  4. Django网站管理、后台、模型(Admin)

    1. 启动wampserver 2. 新建一个名为mysite的项目django-admin.py startproject mysite 3. 新建一个叫做blog的appcd mysitepyth ...

  5. Ubuntu 安装JDK步骤 ,提示没有那个文件或目录

    作为一个程序员,配置环境是最基本的功夫,然而我却捣鼓了一下午,包括安装Ubuntu,安装JDK和配置环境变量. 简单记录下自己的安装过程: 1  下载JDK tar包,使用tar -xzvf jdk* ...

  6. javascript设计模式与开发实践阅读笔记(6)——代理模式

    代理模式:是为一个对象提供一个代用品或占位符,以便控制对它的访问. 代理模式的关键是,当客户不方便直接访问一个对象或者不满足需要的时候,提供一个替身对象来控制对这个对象的访问,客户实际上访问的是替身对 ...

  7. Call and Apply in JavaScript

    Call 和 Apply 方法可以用来代替另一个对象调用一个方法,改变this指向. 1.call -call([thisObj[,arg1[, arg2[,   [,.argN]]]]]) -调用一 ...

  8. 关于《精通移动App测试实战:技术、工具和案例》图书勘误信息

    首先,对由于我们工作的疏忽向<精通移动App测试实战:技术.工具和案例>读者朋友们表示歉意,同时已将这些问题反馈给了出版社编辑同志,再版时将会统一修正: 其次,勘误信息请参看附件pdf文档 ...

  9. Scala 深入浅出实战经典 第78讲:Type与Class实战详解

    王家林亲授<DT大数据梦工厂>大数据实战视频 Scala 深入浅出实战经典(1-87讲)完整视频.PPT.代码下载: 百度云盘:http://pan.baidu.com/s/1c0noOt ...

  10. vim 光标按行移动

    记录一下: [ H/M/L ] 注意:这几个命令是大写的. 使用H/M/L这三个键,可以让光标跳到当前窗口的顶部.中间.和底部,停留在第一个非空字符上.H命令和L命令前也可以加一个数字,但数字的含义不 ...