iOS-Block总结 && 全面解析逆向传值
Posted Juilliardmit
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS-Block总结 && 全面解析逆向传值相关的知识,希望对你有一定的参考价值。
1、block的特点:
block是C语言;
block是一种数据类型、可以当做参数,也可以用做返回值;——总之,对比int的用法用即可(当然,定义的时候,最好跟函数对比);
block是预先准备好的代码块、在需要的时候调用,(需要好好理解“需要时”);
2、定义block
有返回值、有参数:返回类型 ^(blockName)(参数) = ^返回类型(参数列表){///代码 };
无返回值、有参数:void ^(blockName)(参数) = ^(参数列表){///代码 };
无返回值、无参数: void (^blockName)() = ^ { /// 代码实现; };
上面这么多,也记不住:
速记代码快:inlineBlock ,编译器会提示:(根据需要删减就好了);
3、block引用外部变量
在定义block时,如果使用了外部变量,block内部会默认对外部变量做一次copy;
默认情况下,不允许在block内部修改外部变量的值;
在外部变量声明时,使用__block修饰符,则可以在block内部修改外部变量的值;
4、 数组的遍历&排序;
遍历:enumerateObjectsUsingBlock:
所有的参数都已经准备到位,可以直接使用
效率比for高,官方推荐使用;
举例:懒加载
enumerateObjectsUsingBlock遍历:
[tempArray enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL*_Nonnull stop) {
NSDictionary *dict = (NSDictionary*)obj;
Heros *hero = [HerosherosWithDict:dict];
Heros *hero = [HerosherosWithDict:dict];
[ArrMaddObject:hero];
}];
for—IN遍历:
for (NSDictionary*dict in tempArray) {
Heros *heros = [HerosherosWithDict:dict];
[ArrM addObject:heros];
Heros *heros = [HerosherosWithDict:dict];
[ArrM addObject:heros];
}
排序:sortedArrayUsingComparator:
5、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];
- // Do any additional setup after loading the view, typically from a nib.
- }
- - (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)遵守协议;
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
二、通知:
通知方:注册通知;
观察者:注册观察者;
移除观察者对象;
举例:
通知方注册通知、并在恰当的时候发出通知:
- #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];
- // Do any additional setup after loading the view, typically from a nib.
- [[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;
- //self.iamgeView.image = (UIImage *)notifi.object;
- }
- - (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
以上是关于iOS-Block总结 && 全面解析逆向传值的主要内容,如果未能解决你的问题,请参考以下文章
iOS内存管理--Block属性用copy修饰 & 避免循环引用的问题