5 0.OC16-Block
Block
1)、Blocks封装了一段代码,可以在任何时候执行
2)、Blocks可以作为函数参数或者函数的返回值,而其本身又可以带输入参数或返回值。它和传统的函数指针很类似,但是有区别:blocks是inline(内联函数的),并且默认情况下它对局部是只读的
3)、苹果官方建议尽量多用blocks。在多线程、异步任务、集合遍历、集合排序、动画转场用的很多
Blocks的定义
例一
Main.m
// // main.m // OC10-内存管理2-set方法的内存管理 // // Created by qwz on 13-12-9. // Copyright (c) 2013年 renhe. All rights reserved. //
#import <Foundation/Foundation.h> #import "Button.h"
typedef int (^mySum) (int, int) ;
void test(){ //定义了一个block,这个block返回值是int类型,接受两个int类型的参数 int (^Sum) (int, int) = ^(int a, int b){ return a + b; };
int a = Sum(10, 11); NSLog(@"%i", a); }
void test2(){ //__block有2个下划线 __block int c = 15;
//声明了一个block变量 mySum sum = ^(int a, int b){ //如果外部的变量用了__block关键字,就可以在block内部修改这个变量 c = 19; //block可以访问外面定义的变量 NSLog(@"c is %i", c); return a + b; };
NSLog(@"%i", sum(10, 10));
}
int main(int argc, const char * argv[]) {
@autoreleasepool { // test2();
Button *btn = [[[Button alloc] init] autorelease];
btn.block = ^(Button *btn){ NSLog(@"按钮-%@被点击了", btn); };
//模拟按钮点击 [btn click]; } return 0; }
|
Button.h
// // Button.h // OC10-内存管理2-set方法的内存管理 // // Created by qwz on 13-12-10. // Copyright (c) 2013年 renhe. All rights reserved. //
#import <Foundation/Foundation.h>
@classButton;
typedef void (^ButtonBlock) (Button *);
@interface Button : NSObject
@property (nonatomic, assign) ButtonBlock block;
//模拟按钮点击 - (void)click;
@end
|
Button.m
// // Button.m // OC10-内存管理2-set方法的内存管理 // // Created by qwz on 13-12-10. // Copyright (c) 2013年 renhe. All rights reserved. //
#import "Button.h"
@implementation Button - (void)click{ _block(self); }
@end |
相关推荐
OC-block执行完外面的程序才执行方法本身
3. a cell format and extra signals for a 16-bit wide data path for future use The Level 1 Specification was designed to deal with the following PHY layer interfaces : 1. 2. 3. 4. 5. 155.52 Mbps (SONET...
oc中以block形式实现页面间的传值过程
-oc slower duplicate file detection using binary comparisons rather than MD5 hash values -oi ignore diamond compression timestamps when comparing files -os show duplicate files while creating ...
block,对象属性,返回值
-oc slower duplicate file detection using binary comparisons rather than MD5 hash values -oi ignore diamond compression timestamps when comparing files -os show duplicate files while creating ...
一年前网上还找不到关于 inline-block 属性的文章,为了方便大家更好的理解该属性,当时总结整理了篇《display:inline-block的深入理解》。 而现在对于 inline-block 属性运用的需求也越来越多,可惜依旧只有 Fire...
但是,由于display:inline-block最大的不同就是其没有父元素的匿名包裹特性,这使得display:inline-block属性的使用非常自由,可与文字,图片混排,可内嵌block属性元素,可以可以置身于inline水平的元素中。...
display:inline-block 将对象呈递为内联对象,但是对象的内容作为块对象呈递。旁边的内联对象会被呈递在同一行内,允许空格。 但对于这个属性不是所有浏览器都识别。 支持的浏览器有:Opera、Safari。 但很遗憾,最...
block 本质也是一个OC对象,内部也有一个isa指针 block 是封装了函数调用以及函数调用环境的OC对象
异步OC 用Objective-C重写 。 要获得对iOS 7和OS X 10.9的支持,请查看 。 异步糖看起来像这样: Async.background(^{ NSLog ( @" A: This is run on the background " );}).main(^{ NSLog ( @" B: This is run on ...
去掉display:inline-block元素间的多余空白 如下一段代码,display:inline-block元素间的多余空白: XML/HTML Code复制内容到剪贴板 <style type="text/css"> *{margin:0; padding:0;} body{font:12px...
-oc 使用二進位比對代替 MD5 雜湊值, 這會以較慢的速度比對重複的檔案. -oi 比對檔案時忽略高度壓縮的時間戳記 -os 建立影像檔時顯示重複的檔名 (-o 等類似參數不可簡寫為 -ocis) -w 依照數字提供不同的警告等...
-oc 使用二進位比對代替 MD5 雜湊值, 這會以較慢的速度比對重複的檔案. -oi 比對檔案時忽略高度壓縮的時間戳記 -os 建立影像檔時顯示重複的檔名 (-o 等類似參數不可簡寫為 -ocis) -w 依照數字提供不同的警告等...
Inline-block是元素display属性的一个值。这个名字的由来是因为,可以简单的解释为inline+block ;display设置这个值的元素,兼具行内元素( inline elements)跟块级元素(block elements)的特征。块级元素(block...
block,局部变量,全部变量,static的静态变量关系
对iOS中Block的介绍
cd Fucking-Block-Syntax-Autocompletion rake 用法 只需开始输入fuckingBlock ,Xcode就会完成其余工作(并在考虑范围的情况下尽其所能)。 他妈的块Typedef 他妈的块属性 他妈的块作为方法参数
为了弥补inline和block的不足,又扩充了inline-block属性; inline-blcok可以将对象呈递为内联对象,而内容作为块对象呈递。 通俗点讲就是“可定宽高的堆在一起”显示 为什么会有间隙 inline-blcok块之间的不可见...
Swift 中的闭包和 Objective-C 中的Block讲解