Objective-C_Block
一、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的更多相关文章
- Automake
Automake是用来根据Makefile.am生成Makefile.in的工具 标准Makefile目标 'make all' Build programs, libraries, document ...
- Objective C中的ARC的修饰符的使用---- 学习笔记九
#import <Foundation/Foundation.h> @interface Test : NSObject /** * 默认的就是__strong,这里只是做示范,实际使用时 ...
- Objective的字符串拼接 似乎没有Swift方便,但也可以制做一些较为方便的写法
NSString *str1 = @"字符串1"; NSString *str2 = @"字符串2"; //在同样条件下,Objective的字符串拼接 往往只 ...
- [转] 从 C 到 Objective C 入门1
转自: http://blog.liuhongwei.cn/iphone/objective-c/ 进军iPhone开发,最大的难点之一就是怪异的Objective C语法了.不过,了解之后才发现,原 ...
- Objective C运行时(runtime)
#import <objc/runtime.h> void setBeingRemoved(id __self, SEL _cmd) { NSLog(@"------------ ...
- Objective C ARC 使用及原理
手把手教你ARC ,里面介绍了ARC的一些特性, 还有将非ARC工程转换成ARC工程的方法 ARC 苹果官方文档 下面用我自己的话介绍一下ARC,并将看文档过程中的疑问和答案写下来.下面有些是翻译,但 ...
- Objective -C学习笔记之字典
//字典:(关键字 值) // NSArray *array = [NSArray array];//空数组 // NSDictionary *dictionary = [NSDictionary d ...
- 刨根问底Objective-C Runtime
http://chun.tips/blog/2014/11/05/bao-gen-wen-di-objective%5Bnil%5Dc-runtime-(2)%5Bnil%5D-object-and- ...
- Objective-C( Foundation框架 一 字符串)
Objective-C 中核心处理字符串的类是 NSString 与 NSMutableString ,这两个类最大的区别就是NSString 创建赋值以后该字符串的内容与长度不能在动态的更改,除非重 ...
- Objective C类方法load和initialize的区别
Objective C类方法load和initialize的区别 过去两个星期里,为了完成一个工作,接触到了NSObject中非常特别的两个类方法(Class Method).它们的特别之处,在于 ...
随机推荐
- asp.net core 1.0初识
本文将对微软下一代ASP.NET框架做个概括性介绍,方便大家进一步熟悉该框架. 在介绍ASP.NET Core 1.0之前有必要澄清一些产品名称及版本号.ASP.NET Core1.0是微软下一代AS ...
- Java集合类理解
深入Java集合学习系列:http://zhangshixi.iteye.com/blog/674856 http://blog.csdn.net/shf4715/article/details/47 ...
- Wishbone接口通用RAM
/* ************************************************************************************************ ...
- IP地址网段规划
- tracepath 路由跟踪命令
[root@c1 scripts]# tracepath 100.2.4.144 (备注:linux系统) 1: c1.nulige.com (100.2.4.144) 0.047ms pmtu ...
- git中报unable to auto-detect email address
git commit 时报错: ** Please tell me who you are. Run git config --global user.email "you@example. ...
- 【转】The && and || Operator in JavaScript
原文: https://blog.mariusschulz.com/2016/05/25/the-andand-and-operator-in-javascript The && an ...
- 重载delete时的那点事
重载delete时的那点事 C++的异常处理机制就会自动用与被使用 的operator new匹配的operator delete来释放内存(补充一点:在operator new中抛出异常不会导致这样 ...
- Apatche httpd + Django + Mysql web server 搭建
Required: httpd: 2.4.18 django : 1.8.7 mysql: 5.7.10 MySQL-python: 1.2.3 mod_wsgi: 4.4.21 Offical Do ...
- JMeter 七:远程测试
参考:http://jmeter.apache.org/usermanual/remote-test.html http://jmeter.apache.org/usermanual/jmeter_d ...