一、Block语法

Block:块语法,本质上是匿名函数(没有名称的函数),Block变量存放函数的实现,通过Block变量能直接调⽤函数。标准C里面没有Block。C语言的后期扩展版本号。加⼊了匿名函数。C++、JS、Swift等语⾔,有相似语法。叫做闭包。

Block语法和函数指针非常相似。

  • Block类型:int (^)(int)
  • Block变量:myBlock
  • Block值:^ int (int num){ return 7 * num;}
  • 即:^ 返回值类型 (參数列表){函数体} 当中 返回值类型 能够省略。
        /* Block 类型: int (^)(int, int)
* Block 变量: block1
* Block 的值: ^int (int x, int y) { return x + y; };
^ 返回值类型 (參数列表) { 函数体 } (返回值类型能够省略)
*/ /* 1. 无返回值, 无參数的Block */
void (^block3)() = ^()
{
NSLog(@"Hello, World!");
};
block3(); /* Block调用 */ /* 2. 无返回值, 有參数 */
void (^block4)(int, int) = ^(int x, int y)
{
NSLog(@"%d", x + y);
};
block4(3, 45); /* 3. 有返回值, 无參数 */
int (^block5)() = ^()
{
return 100;
};
NSLog(@"%d", block5()); /* 4. 有返回值, 有參数 */
int (^block6)(int, int) = ^(int x, int y)
{
return x > y ? x : y;
};
NSLog(@"%d", block6(3, 5));

二、Block使⽤

int (^block1)(int x, int y) = ^(int x, int y) {
return x + y;
};
int a = block1(32, 34); // block的使⽤和函数指针的使用相似
NSLog(@“%d”, a); // 打印结果:66

Block进⾏typedef

  • typedef int (^BlockType)(int x, int y)

  • 原类型:int (^)(int x, int y)

  • 新类型:BlockType

/* Block typedef */
typedef int(^blockType)(int, int); blockType block1 = ^(int x, int y) {
return x + y;
};
NSLog(@"%d", block1(3, 5));

Block写法

int (^block1)(int x, int y) = ^(int x, int y) {
return x + y;
};
BlockType block1 = ^(int x, int y) {
return x + y;
};

上述两种实现是等价的。

Blcok与局部变量和全局变量

        /* main函数外定义的全局变量 */
int n = 100; /* 知识点4 Block与局部变量 全局变量 */
/* 局部变量 */
int a = 100; int (^block)() = ^() {
return a;
};
NSLog(@"%d", block()); int (^block2)() = ^() {
// a = 200;
return a;
};
/* 总结: 默认情况下, block能够訪问但不能更改局部变量 */ __block int b = 200;
int (^block3)() = ^() {
b = 300;
return b;
};
NSLog(@"%d", block3());
/* 总结: 用__block修饰的局部变量, Block里面能够更改值 */ /* Block 与全局变量 */ int (^block4)() = ^() {
n = 200;
return n;
};
NSLog(@"n = %d", block4()); static int num = 1;
int (^block5)() = ^() {
num = 8;
return num;
};
NSLog(@"num = %d", block5());

Objective-C_Block的更多相关文章

  1. Automake

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

  2. Objective C中的ARC的修饰符的使用---- 学习笔记九

    #import <Foundation/Foundation.h> @interface Test : NSObject /** * 默认的就是__strong,这里只是做示范,实际使用时 ...

  3. Objective的字符串拼接 似乎没有Swift方便,但也可以制做一些较为方便的写法

    NSString *str1 = @"字符串1"; NSString *str2 = @"字符串2"; //在同样条件下,Objective的字符串拼接 往往只 ...

  4. [转] 从 C 到 Objective C 入门1

    转自: http://blog.liuhongwei.cn/iphone/objective-c/ 进军iPhone开发,最大的难点之一就是怪异的Objective C语法了.不过,了解之后才发现,原 ...

  5. Objective C运行时(runtime)

    #import <objc/runtime.h> void setBeingRemoved(id __self, SEL _cmd) { NSLog(@"------------ ...

  6. Objective C ARC 使用及原理

    手把手教你ARC ,里面介绍了ARC的一些特性, 还有将非ARC工程转换成ARC工程的方法 ARC 苹果官方文档 下面用我自己的话介绍一下ARC,并将看文档过程中的疑问和答案写下来.下面有些是翻译,但 ...

  7. Objective -C学习笔记之字典

    //字典:(关键字 值) // NSArray *array = [NSArray array];//空数组 // NSDictionary *dictionary = [NSDictionary d ...

  8. 刨根问底Objective-C Runtime

    http://chun.tips/blog/2014/11/05/bao-gen-wen-di-objective%5Bnil%5Dc-runtime-(2)%5Bnil%5D-object-and- ...

  9. Objective-C( Foundation框架 一 字符串)

    Objective-C 中核心处理字符串的类是 NSString 与 NSMutableString ,这两个类最大的区别就是NSString 创建赋值以后该字符串的内容与长度不能在动态的更改,除非重 ...

  10. Objective C类方法load和initialize的区别

    Objective C类方法load和initialize的区别   过去两个星期里,为了完成一个工作,接触到了NSObject中非常特别的两个类方法(Class Method).它们的特别之处,在于 ...

随机推荐

  1. 客户端Git的常用命令

    (1)git clone 服务器用户名@服务器IP:~/Git目录/.git 功能:下载服务器端Git仓库中的文件或目录到本地当前目录. (2)git status 功能:查看Git仓库中的文件状态. ...

  2. javascript常用排序算法实现

    毕业后,由于工作中很少需要自已去写一些排序,所以那些排序算法都忘得差不多了,不过排序是最基础的算法,还是不能落下啦,于是找了一些资料,然后用Javascript实现了一些常用的算法,具体代码如下: & ...

  3. linux系统下mysql跳过密码验证登录和创建新用户

    修改MySQL的登录设置: # vi /etc/my.cnf 在[mysqld]的段中加上一句:skip-grant-tables 例如: [mysqld] datadir=/var/lib/mysq ...

  4. Nginx + FastCGI 程序(C/C++)搭建高性能web service的demo

    http://blog.csdn.net/chdhust/article/details/42645313 Nginx + FastCGI 程序(C/C++)搭建高性能web service的Demo ...

  5. easyui中combotree只能选子选项,父级不被选中

    前言 前几天面试遇到一个需求(easyui中combotree只能选子选项,父级不被选中),回来特意整理下,大概的思想是如果该tree的节点被选中是判定一下是否有子节点,如果没有就说明是最终节点了,步 ...

  6. Browsers 之 弹出窗口阻止问题

    主要关注两个地方: 1.Microsoft Edge 浏览器: 浏览器 “ 设置 → 查看高级设置 ”,找到 “ 阻止弹出窗口 ”,关闭. 2.IE浏览器 [1] “ 工具 → 弹出窗口阻止程序 ”, ...

  7. java泛型介绍

    一.泛型初衷 Java集合不会知道我们需要用它来保存什么类型的对象,所以他们把集合设计成能保存任何类型的对象,只要就具有很好的通用性.但这样做也带来两个问题: –集合对元素类型没有任何限制,这样可能引 ...

  8. android 蓝牙SPP协议通信

    准备 1.蓝牙串行端口基于SPP协议(Serial Port Profile),能在蓝牙设备之间创建串口进行数据传输 2.SPP的UUID:00001101-0000-1000-8000-00805F ...

  9. iOS工程中的info.plist文件的完整研究

    原地址:http://blog.sina.com.cn/s/blog_947c4a9f0100zf41.html 们建立一个工程后,会在Supporting files下面看到一个"工程名- ...

  10. Python中的关键字的用法

    Python有哪些关键字 -Python常用的关键字 and, del, from, not, while, as, elif, global, or, with, assert, else, if, ...