一、block的特色:
block是C語言;
block是一種數據類型、能夠當作參數,也能夠用作返回值;——總之,對比int的用法用便可(固然,定義的時候,最好跟函數對比);
block是預先準備好的代碼塊、在須要的時候調用,(須要好好理解「須要時」);
二、定義block
有返回值、有參數:返回類型 ^(blockName)(參數) = ^返回類型(參數列表){///代碼 };
無返回值、有參數:void ^(blockName)(參數) = ^(參數列表){///代碼 };
無返回值、無參數:
void (^blockName)() = ^ { /// 代碼實現; };
上面這麼多,也記不住:
速記代碼快:inlineBlock ,編譯器會提示:(根據須要刪減就行了);
三、block引用外部變量
在定義block時,若是使用了外部變量,block內部會默認對外部變量作一次copy;
默認狀況下,不容許在block內部修改外部變量的值;
在外部變量聲明時,使用__block修飾符,則能夠在block內部修改外部變量的值;
四、
數組的遍歷&排序;
遍歷:enumerateObjectsUsingBlock:
全部的參數都已經準備到位,能夠直接使用
效率比for高,官方推薦使用;
舉例:懶加載
enumerateObjectsUsingBlock遍歷:
[tempArray enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL*_Nonnull stop) {
NSDictionary
*dict = (NSDictionary*)obj;
Heros *hero = [HerosherosWithDict:dict];
[ArrMaddObject:hero];
}];
for—IN遍歷:
for
(NSDictionary*dict in tempArray) {
Heros *heros = [HerosherosWithDict:dict];
[ArrM addObject:heros];
}
排序:sortedArrayUsingComparator:
五、block的數據的逆向傳值
被調用方:
準備塊代碼;
調用方:
定義塊代碼屬性,在適當的時候調用block;
舉例:(如下三個舉例實現了自定義NSOperation,異步下載一張圖片,並在主線程中顯示)
調用方:
定義塊代碼屬性
- #import <Foundation/Foundation.h>
- #import <UIKit/UIKit.h>
-
- @class YSCNSOperationOP;
-
- typedef void(^setUpUIImage)(YSCNSOperationOP *);
-
- @interface YSCNSOperationOP : NSOperation
-
- @property (nonatomic, copy) NSString *urlString;
- @property (nonatomic, strong) UIImage *image;
-
- @property (nonatomic, copy) setUpUIImage myBlock;
- - (void)setUpUIImage:(setUpUIImage )block;
-
- @end
在適當的時候執行:
- #import "YSCNSOperationOP.h"
- @implementation YSCNSOperationOP
- - (void)main {
- @autoreleasepool {
- UIImage *image = [self downLoadImage:self.urlString];
- self.image = image;
- dispatch_async(dispatch_get_main_queue(), ^{
- self.myBlock(self);
- });
- }
- }
- - (UIImage *)downLoadImage:(NSString *)urlString{
-
- NSURL *url = [NSURL URLWithString:urlString];
- NSData *data = [NSData dataWithContentsOfURL:url];
- UIImage *image = [UIImage imageWithData:data];
- return image;
- }
- - (void)setUpUIImage:(setUpUIImage )block {
- if (block) {
- self.myBlock = block;
- }
- }
- @end
被調用方:
準備代碼塊:
- #import "ViewController.h"
- #import "YSCNSOperationOP.h"
- @interface ViewController ()
- @property (weak, nonatomic) IBOutlet UIImageView *iamgeView;
- @end
- @implementation ViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
-
- }
- - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
- YSCNSOperationOP *yscOp = [[YSCNSOperationOP alloc] init];
- yscOp.urlString = @"http://h.hiphotos.baidu.com/image/pic/item/9825bc315c6034a8094ace24c9134954082376ee.jpg";
- [yscOp setUpUIImage:^(YSCNSOperationOP *op) {
- self.iamgeView.image = op.image ;
- }];
- NSOperationQueue *queue = [[NSOperationQueue alloc] init];
- [queue addOperation:yscOp];
- }
- @end
其它方式的逆向傳值:
1、代理:
代理方:
1)遵照協議;
2)設置代理;
3)實現代理方法;
委託方:
1)定義協議;
2)代理屬性;
3)在須要的時候通知代理;‘
舉例
委託方:定義協議;
代理屬性;
- #import <Foundation/Foundation.h>
- #import <UIKit/UIKit.h>
- @class YSCNSOperation;
- @protocol YSCNSOperationDelegate <NSObject>
- - (void)yscNSOperation:(YSCNSOperation *)operation withImage:(UIImage *)image;
- @end
- @interface YSCNSOperation : NSOperation
- @property (nonatomic, copy) NSString *urlString;
- @property (nonatomic, strong) UIImage *image;
- @property (nonatomic, weak) id<YSCNSOperationDelegate> delegate;
- @end
在須要的時候通知代理:
- #import "YSCNSOperation.h"
- @implementation YSCNSOperation
- - (void)main {
- @autoreleasepool {
- UIImage *image = [self downLoadImage:self.urlString];
- self.image = image;
- dispatch_async(dispatch_get_main_queue(), ^{
- if ([self.delegate respondsToSelector:@selector(yscNSOperation:withImage:)]) {
- [self.delegate yscNSOperation:self withImage:image];
- }
- });
- }
- }
- - (UIImage *)downLoadImage:(NSString *)urlString{
- NSURL *url = [NSURL URLWithString:urlString];
- NSData *data = [NSData dataWithContentsOfURL:url];
- UIImage *image = [UIImage imageWithData:data];
- return image;
- }
- @end
代理方:
- #import "ViewController.h"
- #import "YSCNSOperation.h"
- @interface ViewController () <YSCNSOperationDelegate>
- @property (weak, nonatomic) IBOutlet UIImageView *iamgeView;
- @end
- @implementation ViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- }
- - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- YSCNSOperation *yscOp = [[YSCNSOperation alloc] init];
- yscOp.delegate = self;
- yscOp.urlString = @"http://h.hiphotos.baidu.com/image/pic/item/9825bc315c6034a8094ace24c9134954082376ee.jpg";
- NSOperationQueue *queue = [[NSOperationQueue alloc] init];
- [queue addOperation:yscOp];
- });
- }
- - (void)yscNSOperation:(YSCNSOperation *)operation withImage:(UIImage *)image {
- self.iamgeView.image = operation.image;
- }
- @end
2、通知:
通知方:註冊通知;
觀察者:註冊觀察者;
移除觀察者對象;
舉例:
通知方註冊通知、並在恰當的時候發出通知:
- #import "YSCNSOperation.h"
- @implementation YSCNSOperation
- - (void)main {
- @autoreleasepool {
- UIImage *image = [self downLoadImage:self.urlString];
- self.image = image;
- dispatch_async(dispatch_get_main_queue(), ^{
- [[NSNotificationCenter defaultCenter] postNotificationName:@"setUpUI" object:self];
- });
- }
- }
- - (UIImage *)downLoadImage:(NSString *)urlString{
-
- NSURL *url = [NSURL URLWithString:urlString];
- NSData *data = [NSData dataWithContentsOfURL:url];
- UIImage *image = [UIImage imageWithData:data];
- return image;
- }
- @end
觀察者:註冊觀察者、移除觀察者
- #import "ViewController.h"
- #import "YSCNSOperation.h"
- @interface ViewController ()
- @property (weak, nonatomic) IBOutlet UIImageView *iamgeView;
- @end
- @implementation ViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
-
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(lookNotifi:) name:@"setUpUI" object: nil nil];
- }
- - (void)lookNotifi:(NSNotification *)notifi{
- YSCNSOperation *op= (YSCNSOperation *)notifi.object;
- self.iamgeView.image = op.image;
-
- }
- - (void)dealloc {
- [[NSNotificationCenter defaultCenter] removeObserver:self];
- }
- - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
- YSCNSOperation *yscOp = [[YSCNSOperation alloc] init];
- yscOp.urlString = @"http://h.hiphotos.baidu.com/image/pic/item/9825bc315c6034a8094ace24c9134954082376ee.jpg";
- NSOperationQueue *queue = [[NSOperationQueue alloc] init];
- [queue addOperation:yscOp];
- }
- @end