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 ...
随机推荐
- SpiderMonkey的使用
基于 C 语言的 JavaScript 引擎探索 http://www.ibm.com/developerworks/cn/linux/l-cn-spidermonkey/ https://devel ...
- C错误异常处理,异常处理
预处理器标识#error的目的是什么啊? 指令 用途 # 空指令,无任何效果 #include 包含一个源代码文件 #define 定义宏 #undef 取消已定义的宏 #if 如果给定条件为真,则编 ...
- HBase目录
1. HBase介绍及简易安装(转) 2. java操作Hbase实例 3. HBase入门基础教程之单机模式与伪分布式模式安装(转) 4. HBase教程 5. 用Java操纵HBase数据库(新建 ...
- fdisk 分区格式化为ext4格式分区
第一步:添加硬盘/新建分区(fdisk) 第二步:格式化分区(mkfs.ext4) 第三步:加载分区(mount) 1.第一步:添加硬盘/新建分区(fdisk) a.查看当前系统所有硬盘及分区情况:f ...
- 【Dagger2】 案例大全
只有Inject是不可以的,必须有Component public class Test { @Inject Person person; private void test() { System.o ...
- 25个精致的IPHONE 6和APPLE WATCH原型下载!
自iPhone 6 和Apple Watch那场的发布会后,很多设计师开始创作相关的原型素材,值得感谢的是,这些作品有好多都无私分享到了网上,不仅可个人使用,也可商用.今天我们收集了从这组免费的素材中 ...
- 比较windows phone程序启动和android程序启动原理
windows phone 程序是如何启动的了,他和android程序有什么区别,我们重点从native code 层面来分析 在windows phone 程序启动的时候是: 在XAML中使用应用程 ...
- TextBox_TextChanged
private void TextBox_TextChanged(object sender, TextChangedEventArgs e) { TextBox textBox = sender a ...
- 修改linux的文件时,如何快速找到要修改的内容并修改
修改linux系统下的文件时,如果文件内容很多,不容易找到需要修改的内容,下面详细介绍linux系统下如何快速修改文件. 工具/原料 linux系统 方法/步骤 在linux系统下,找到需 ...
- ACM-尼姆博弈之取(m堆)石子游戏——hdu2176
取(m堆)石子游戏 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...