Block编程注意的问题
- - (void)testAccessVariable
- {
- NSInteger outsideVariable = 10;
- //__block NSInteger outsideVariable = 10;
- NSMutableArray * outsideArray = [[NSMutableArray alloc] init];
- void (^blockObject)(void) = ^(void){
- NSInteger insideVariable = 20;
- KSLog(@" > member variable = %d", self.memberVariable);
- KSLog(@" > outside variable = %d", outsideVariable);
- KSLog(@" > inside variable = %d", insideVariable);
- [outsideArray addObject:@"AddedInsideBlock"];
- };
- outsideVariable = 30;
- self.memberVariable = 30;
- blockObject();
- KSLog(@" > %d items in outsideArray", [outsideArray count]);
- }
- > member variable = 30
- > outside variable = 10
- > inside variable = 20
- > 1 items in outsideArray
- typedef NSString* (^IntToStringConverter)(id self, NSInteger paramInteger);
- - (NSString *) convertIntToString:(NSInteger)paramInteger
- usingBlockObject:(IntToStringConverter)paramBlockObject
- {
- return paramBlockObject(self, paramInteger);
- }
- typedef NSString* (^IntToStringInlineConverter)(NSInteger paramInteger);
- - (NSString *) convertIntToStringInline:(NSInteger)paramInteger
- usingBlockObject:(IntToStringInlineConverter)paramBlockObject
- {
- return paramBlockObject(paramInteger);
- }
- IntToStringConverter independentBlockObject = ^(id self, NSInteger paramInteger) {
- KSLog(@" >> self %@, memberVariable %d", self, [self memberVariable]);
- NSString *result = [NSString stringWithFormat:@"%d", paramInteger];
- KSLog(@" >> independentBlockObject %@", result);
- return result;
- };
- - (void)testAccessSelf
- {
- // Independent
- //
- [self convertIntToString:20 usingBlockObject:independentBlockObject];
- // Inline
- //
- IntToStringInlineConverter inlineBlockObject = ^(NSInteger paramInteger) {
- KSLog(@" >> self %@, memberVariable %d", self, self.memberVariable);
- NSString *result = [NSString stringWithFormat:@"%d", paramInteger];
- KSLog(@" >> inlineBlockObject %@", result);
- return result;
- };
- [self convertIntToStringInline:20 usingBlockObject:inlineBlockObject];
- }
- @interface KSViewController ()
- {
- id _observer;
- }
- @end
- @implementation KSViewController
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- // Do any additional setup after loading the view, typically from a nib.
- KSTester * tester = [[KSTester alloc] init];
- [tester run];
- _observer = [[NSNotificationCenter defaultCenter]
- addObserverForName:@"TestNotificationKey"
- object:nil queue:nil usingBlock:^(NSNotification *n) {
- NSLog(@"%@", self);
- }];
- }
- - (void)dealloc
- {
- if (_observer) {
- [[NSNotificationCenter defaultCenter] removeObserver:_observer];
- }
- }
- __weak KSViewController * wself = self;
- _observer = [[NSNotificationCenter defaultCenter]
- addObserverForName:@"TestNotificationKey"
- object:nil queue:nil usingBlock:^(NSNotification *n) {
- KSViewController * sself = wself;
- if (sself) {
- NSLog(@"%@", sself);
- }
- else {
- NSLog(@" dealloc before we could run this code.");
- }
- }];
- - (id) getBlockArray
- {
- int val = 10;
- return [[NSArray alloc] initWithObjects:
- ^{ KSLog(@" > block 0:%d", val); }, // block on the stack
- ^{ KSLog(@" > block 1:%d", val); }, // block on the stack
- nil];
- // return [[NSArray alloc] initWithObjects:
- // [^{ KSLog(@" > block 0:%d", val); } copy], // block copy to heap
- // [^{ KSLog(@" > block 1:%d", val); } copy], // block copy to heap
- // nil];
- }
- - (void)testManageBlockMemory
- {
- id obj = [self getBlockArray];
- typedef void (^BlockType)(void);
- BlockType blockObject = (BlockType)[obj objectAtIndex:0];
- blockObject();
- }
Block编程注意的问题的更多相关文章
- Block编程值得注意的那些事儿
[深入浅出Cocoa]Block编程值得注意的那些事儿 [深入浅出Cocoa]Block编程值得注意的那些事儿 罗朝辉 (http://www.cnblogs.com/kesalin/) 本文遵循 ...
- (译)IOS block编程指南 1 介绍
Introduction(介绍) Block objects are a C-level syntactic and runtime feature. They are similar to stan ...
- 【ios开发】Block编程
1 什么是block iOS SDK 4.0开始,Apple引入了block这一特性.字面上说,block就是一个代码块,但是它的神奇之处在于在内联(inline)执行的时候(这和C++很像)还可以传 ...
- IOS多线程之Block编程
1 什么是block iOS SDK 4.0開始,Apple引入了block这一特性.字面上说,block就是一个代码块.可是它的奇妙之处在于在内联(inline)运行的时候(这和C++非常像)还 ...
- (译文)IOS block编程指南 4 声明和创建blocks
Declaring and Creating Blocks (声明和创建blocks) Declaring a Block Reference (声明一个block引用) Block variable ...
- (译文)IOS block编程指南 3 概念总览
Conceptual Overview(概览) Block objects provide a way for you to create an ad hoc function body as an ...
- (译)IOS block编程指南 2 block开始
Getting Started with Blocks(开始block) The following sections help you to get started with blocks usin ...
- IOS学习之十七:Grand Central Dispatch(GCD)编程基础
IOS学习之十七:Grand Central Dispatch(GCD)编程基础 有过编程经验的人,基本都会接触到多线程这块. 在java中以及Android开发中,大量的后台运行,异步消息队列, ...
- 【block第四篇】实现
-------------------------------------------欢迎查看block连载博客[专栏]--------------------------------------[b ...
随机推荐
- NFS CIFS SAMBA 的联系和区别
Common Internet File System, CIFS Server Message Block, SMB Network File System, NFS 在早期网络世界当中,档案数据在 ...
- Java并发编程里的volatile。Java内存模型核CPU内存架构的对应关系
CPU内存架构:https://www.jianshu.com/p/3d1eb589b48e Java内存模型:https://www.jianshu.com/p/27a9003c33f4 多线程下的 ...
- 【BZOJ】【1006】【HNOI2008】神奇的国度
弦图最小染色/MCS算法 Orz PoPoQQQ (UPD:ydc的写法好像更熟悉一些……(类似堆优化的Dij啊~ 先留个坑……明天再看一看……感觉好神奇>_<(完美消除序列之于弦图 就 ...
- iOS开发--开源库
图像: 1.图片浏览控件MWPhotoBrowser 实现了一个照片浏览器类似 iOS 自带的相册应用,可显示来自手机的图片或者是网络图片,可自动从网络下载图片并进行缓存.可对图片进行缩 ...
- Steps to configure a FileShare Quorum Witness for Windows Failover Cluster
Step 1: Step 2: Step 3: Step 4: You must use the wizard to create the file share. Step 5: to make su ...
- Adapter 适配器模式 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- 【Ansible】Playbook实例
Learn to build Ansible playbooks with our guide, one step at a time In our previous posts, we introd ...
- 敏捷开发方法XP的12个最佳实践
极限编程(eXtreme Programming,简称XP)是一种轻量级.高效.低风险.柔性.可预测的.科学的软件开发方法,其特性包含在12个最佳实践中. 1. 计划游戏 ( Planning Ga ...
- [Algorithm] Tree: Lowest Common Ancestor
By given a tree structure, task is to find lowest common ancestor: For example, LCA(4, 5) --> > ...
- bash參考手冊之六(Bash特性)
6 Bash 特性 这部分描写叙述Bash独有的特性. * 调用Bash : Bash能够接受的命令行选项. * Bash启动文件 : Bash何时及怎样运行脚本. * 交互Shell : 什么 ...