浅用block 转
block是一门有用的大后期学问。现在我只是列出一点基本用法。
1.快速枚举(Enumeration)
通常是和NSArray, NSDictionary, NSSet, NSIndexSet放在一起用。
当和以上这两种东西放在一起用时,通常block有两种用处。(代码为实例操作)
i. 第一种block用法是枚举后,对每个枚举对象进行一些操作,block返回值为void
ii. 第二种枚举对象的index,当然这些枚举对象是通过某些测试后才返回的。
// 第一种用法 返回值为0,对每一个对象进行相应操作
NSArray *array = [NSArray arrayWithObjects:@"one", @"two", @"three", nil];
NSMutableArray *mArray = [NSMutableArray alloc]init];
[array enumerateObjectsWithOptions:NSEnumerationConcurrent | NSEnumerationRevese usingBlock:^(id obj, NSUInteger idx, BOOL *stop){
[mArray addObject:obj];
}];
// 第二种用法,返回的是一个通过passTest的index
NSArray *array = [NSArray arrayWithObjects:@"one", @"two", @"three", nil];
NSInteger * index = [array indexOfObjectWithOptions:NSEnumerationConcurrent passingTest: (BOOL)^(id obj, NSUInteger idx, BOOL *stop){
NSString *string = (NSString *)obj;
return [string hasPrefix:@"O"];
}];
2.GCD 多线程
这里就直接举一个例子了。假设我们有一个UILable,现在NSJSONSerialization 来解析某个某个地址的json file。现在要实现的是用这个UILable来表示载入状态,如载入前它的text属性应该是@"is loading", 载入后是@"has loaded" 具体的看代码
//在该UILable的viewController的ViewDidAppear 中
statusLable.text = @"is loading";
dispatch_queue_t qq = dispatch_queue_create("com.sayALittle', nil);
// 即使你使用了ARC,也记得需要自己写一个dealloc方法来释放queue,但是这里面就不需要用[super dealloc]
dispatch_async(queue, ^{
NSError *error;
//假设本地有个array用来接收JSON解析出来的Array
self.array = [NSJONSerialization JSONObjectWithData:[NSData dataWithContentsOfURL:someURL options:kNilOptions error:&error];
dispatch_async(dispatch_get_main_queue(), ^{
statusLable.text = @"has loaded";
});
});
- (void)dealloc
{
dispatch_release(queue);
}
//因为UI的事情一直都是在主线程内完成的,所以这里就在解析数据后,马上在主线程中更新了。
//如前面说的,要一个dealloc来释放queue
国外友人的罗列的基本用法
NSArray
- enumerateObjectsUsingBlock – Probably the Block method I use the most, it basically is a simpler, cleaner foreach.
- enumerateObjectsAtIndexes:usingBlock: – Same as enumerateObjectsUsingBlock: except you can enumerate a specific range of items in the array instead of all the items. The range of items to enumerate is passed via the indexSet parameter. // 这里indexes可以用NSMakeRange(0, 3)这种来自我创建 以及+ (id)indexSetWithIndexesInRange:(NSRange)indexRange
- indexesOfObjectsPassingTest: – The Block returns an indexset of the the objects that pass a test specified by the Block. Useful for looking for a particular group of objects.
NSDictionary
- enumerateKeysAndObjectsUsingBlock: – Enumerates through a dictionary, passing the Block each key and object.
- keysOfEntriesPassingTest: – Returns a set of the keys corresponding to objects that pass a test specified by the Block.
UIView
- animateWithDuration:animations: – UIViewAnimation Block, useful for simple animations.
- animateWithDuration:completion: – Another UIViewAnimation Block, this version adds a second Block parameter for callback code when the animation code has completed.
Grand Central Dispatch
- dispatch_async – This is the main function for async GCD code.
转 : http://www.cnblogs.com/davidxie/archive/2012/08/23/2652214.html
浅用block 转的更多相关文章
- 浅谈 block(1) – clang 改写后的 block 结构
这几天为了巩固知识,从 iOS 的各个知识点开始学习,希望自己对每一个知识理解的更加深入的了解.这次来分享一下 block 的学习笔记. block 简介 block 被当做扩展特性而被加入 GCC ...
- 浅入“Block Formatting Context”
本文主要是针对BFC特性的应用,至于什么是BFC,可以参看MDN的简介: https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Block_fo ...
- 浅谈Block传值-匿名函数(代码块)
block传值是从后往前传值---代理也是 // 使用block时, 不能访问self, 也不能直接访问属性, self.属性, 用self调用方法; 只要这样做了, block都会对其强引用一份, ...
- 浅议block实现原理,block为什么使用copy关键字?
1.block是一个特殊的oc对象,建立在栈上,而不是堆上,这么做一个是为性能考虑,还有就是方便访问局部变量. 2.默认Block使用到的局部变量会被copy,而不是retain.所以,他无法改变局部 ...
- 浅谈block, inline和inline-block的区别
block 块元素 inline 内联元素 常见的块元素有:div, p, h1~h6, table, form, ol, ul等 常见的内联元素有:span, a, strong, em, l ...
- iOS开展block说明
源代码下载 浅谈block使用方法 对于block他用着确实方便,好多人都非常迷茫,这里写了一个Demo解说block的使用方法 好多人都觉得block是用于后一个界面向前一个界面传值用的,事实上更详 ...
- 使用 libffi 实现 AOP
核心还是利用oc消息的查找派发机制,进行类结构的动态修改,用新函数替换老函数,然后再调用老函数. 前言 众所周知,使用runtime的提供的接口,我们可以设定原方法的IMP,或交换原方法和目标 ...
- IOS 浅谈闭包block的使用
前言:对于ios初学者,block通常用于逆向传值,遍历等,会使用,但是可能心虚,会感觉block很神秘,那么下面就一起来揭开它的面纱吧. ps: 下面重点讲叙了闭包的概念,常用的语法,以及访问变量, ...
- iOS开发-由浅至深学习block
关于block 在iOS 4.0之后,block横空出世,它本身封装了一段代码并将这段代码当做变量,通过block()的方式进行回调.这不免让我们想到在C函数中,我们可以定义一个指向函数的指针并且调用 ...
随机推荐
- performance 判断页面是以哪种方式进入的
if (window.performance) { console.info("window.performance is supported"); console.log(per ...
- 【读书笔记】Android的Ashmem机制学习
Ashmem是安卓在linux基础上添加的驱动模块,就是说安卓有linux没有的功能. Ashmem模块在内核层面上实现,在运行时库和应用程序框架层提供了访问接口.在运行时库层提供的是C++接口,在应 ...
- 一步一步学习IdentityServer4 (5) .NETCore2.0 Swagger
首先添加nuget: Swashbuckle.AspNetCore services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new ...
- day6面向对象
面向对象介绍(http://www.cnblogs.com/alex3714/articles/5188179.htm) 世界万物,皆可分类 世界万物,皆为对象 只要是对象,就 ...
- MyEclipse个性化设置
1.修改项目文件默认编码 Note:myEclipse默认的编码是GBK, 也就是未设置编码格式的文件都默认使用GBK进行编码, 而更糟糕的是JSP.JavaScriptt默认编码竟然是ISO-885 ...
- ubuntu编译安装postgresql
闲着没事用源码编译安装了postgresql,遇到了不少故障,记录一下. 1:用./configure配置时发生错误.看信息说是缺少相关包.有什么readline,zlip等. 我配置的很简单,只是配 ...
- Django实战(7):改造ProductList界面
有了上一节关于Django模板的基础,改造界面就很容易理解了.将界面设计师设计的页面中的内容根据复用程度分别放到基础模板base.html和专用模板productlist.html中. depot/t ...
- thinkphp调整框架核心目录think的位置
thinkphp的核心目录即框架文件可以放在项目之外的目录,这点手册上有提到,放在项目之外的地方可以方便其他项目共用一个框架文件. 在入口文件的index.php中,在导入框架目录这一行,可以直接修改 ...
- vue 父子间组件传值
1.父组件向子组件传值: 实例截图: 实例代码: /*子组件代码*/ //child.vue <template> <div style="border: 1px soli ...
- python获取当前系统的桌面的路径
一,用内置的winreg(推荐) import winregdef get_desktop(): key = winreg.OpenKey(winreg.HKEY_CURRENT_USER,\ ...