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 ...
随机推荐
- CVPR 2015 papers
CVPR2015 Papers震撼来袭! CVPR 2015的文章可以下载了,如果链接无法下载,可以在Google上通过搜索paper名字下载(友情提示:可以使用filetype:pdf命令). Go ...
- Java读取properties配置文件经常用法
在开发中对properties文件的操作还是蛮常常的.所以总结了几种操作方法,为后面的开发能够进行參考. 1.通过java.util.ResourceBundle类来读取 这边測试用到了枚举类进行传入 ...
- Visual GC(监控垃圾回收器)
Java VisualVM默认没有安装Visual GC插件,需要手动安装,JDK的安装目录的bin目露下双击jvisualvm.exe,即可打开Java VisualVM,点击菜单栏 工具-> ...
- @JVM垃圾回收机制的一些概念
数据类型 Java虚拟机中,数据类型可以分为两类:基本数据类型和引用数据类型 .基本类型的变量保存的值就是数值本身:而引用类型的变量保存引用值."引用值"代表了某个对象的引用,而不 ...
- data-stream-as-disjoint-intervals
https://leetcode.com/problems/data-stream-as-disjoint-intervals/ /** * Definition for an interval. * ...
- c++学习之友元
最近工作好累呀,晚上总是失眠,自学c++的步骤都放慢了,本来之前看c++ primer的,结果这本书讲的太细节了,初学者不是很好把握.所以我又重新找了个教程,比较适合初学者.今天学习到友元函数和友元类 ...
- vue-router各个属性的作用及用法
vue-router是vue单页面开发的路由,就是决定页面跳转的! <router-link> 组件支持用户在具体有路由功能的应用中(点击)导航.通过to属性指定目标地址. Props 属 ...
- TJ Holowaychuk是怎样学习编程的?
TJ Holowaychuk是怎样学习编程的? 学习了:https://blog.csdn.net/wozaixiaoximen/article/details/49507111 Q:TJ Holow ...
- spark shuffle过程分析
spark shuffle流程分析 回到ShuffleMapTask.runTask函数 如今回到ShuffleMapTask.runTask函数中: overridedef runTask(cont ...
- 解决百度编辑器在编辑视频时src丢失的问题
问题描述:使用的是最新的UEditor 1.4.3.3版本,在上传完视频后,编辑的时候出现视频的src丢失的问题 解决方式:修改ueditor.config.js文件,将 img: ['src', ' ...