一、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. 详解java中的数据结构

    线性表,链表,哈希表是常用的数据结构,在进行Java开发时,JDK已经为我们提供了一系列相应的类来实现基本的数据结构.这些类均在java.util包中.本文试图通过简单的描述,向读者阐述各个类的作用以 ...

  2. 线程局部存储(TLS)

    线程局部存储(TLS) 2011-10-11 09:59:28|  分类: Win32---API |  标签:tls   |举报 |字号 订阅   什么是线程局部存储 众所周知,线程是执行的单元,同 ...

  3. EPF与Myeclipse 增强代码自动智能提示

    摘自: http://blog.csdn.net/ylchou/article/details/7639467 数字证书文件,导入用. EPF文件是著名的软件开发工具——Eclipse(IDE)的配置 ...

  4. HDU 3726 Graph and Queries treap树

    题目来源:HDU 3726 Graph and Queries 题意:见白书 思路:刚学treap 參考白皮书 #include <cstdio> #include <cstring ...

  5. 让所有窗体都从DevExpress.XtraEditors.XtraForm继承

    让所有窗体都从DevExpress.XtraEditors.XtraForm继承. 第一步:在项目中添加 引用: DevExpress.BonusSkins.v14// DevExpress.Offi ...

  6. /dev/null 的含义和用途

    /dev/null 代表空设备文件,它等价于一个仅仅写文件,全部写入它的内容都会永远丢失.而尝试从它那儿读取内容则什么也读不到. 0:表示键盘输入(stdin) 1:表示标准输出(stdout),系统 ...

  7. Java补漏(一)

     第一章前言 在学长的建议下,为了弥补之前学Java漏下的或者不是非常清楚的知识点,买了本蛮好的教科书-<Java学习笔记(JDK6)>,正式又一次学习.为了记下一些让我恍然大悟的知识 ...

  8. ssm整合(Spring+SpringMVC+Mybatis)

    一.Spring Spring致力于提供一种方法管理你的业务对象.IOC容器,它可以装载bean(也就是我们java中的类,当然也包括service dao里面的),有了这个机制,我们就不用在每次使用 ...

  9. 解决Xcode 6 编译Cocos2d-x iOS项目失败

    在Xcode 6 beta里编译Cocos2d-x iOS项目时可能会失败,提示如下错误: Undefined symbols for architecture i386: "_fwrite ...

  10. mysql导入数据乱码的解决

    #mysql -uroot -p -hlocalhost --default-character-set=utf8; mysql>use db_name; mysql>source /ho ...