简体   繁体   English

这是Objective-C的障碍吗?

[英]Is this an objective-c block?

I came across some code in an opensource library called SIAlertView and I am not sure what it is doing? 我在名为SIAlertView的开源库中遇到了一些代码,但不确定它在做什么? Specifically the two lines below? 具体来说是下面两行? What is SIAleartViewHandler as it is not a class in the SIAlertView library? 什么是SIAleartViewHandler,因为它不是SIAlertView库中的类?

typedef void(^SIAlertViewHandler)(SIAlertView *alertView);
@property (nonatomic, copy) SIAlertViewHandler willShowHandler;

SIAlertView.h SIAlertView.h

typedef void(^SIAlertViewHandler)(SIAlertView *alertView);
@property (nonatomic, copy) SIAlertViewHandler willShowHandler;
@property (nonatomic, copy) SIAlertViewHandler didShowHandler;
@property (nonatomic, copy) SIAlertViewHandler willDismissHandler;
@property (nonatomic, copy) SIAlertViewHandler didDismissHandler;

SIAlertView.m SIAlertView.m

[self transitionInCompletion:^{
    if (self.didShowHandler) {
        self.didShowHandler(self);
    }
    [[NSNotificationCenter defaultCenter] postNotificationName:SIAlertViewDidShowNotification object:self userInfo:nil];

    [SIAlertView setAnimating:NO];

    NSInteger index = [[SIAlertView sharedQueue] indexOfObject:self];
    if (index < [SIAlertView sharedQueue].count - 1) {
        [self dismissAnimated:YES cleanup:NO]; // dismiss to show next alert view
    }
}];

#pragma mark - SIAlertItem

@interface SIAlertItem : NSObject

@property (nonatomic, copy) NSString *title;
@property (nonatomic, assign) SIAlertViewButtonType type;
@property (nonatomic, copy) SIAlertViewHandler action;

@end

@implementation SIAlertItem

@end

The first line ( typedef void(^SIAlertViewHandler)(SIAlertView *alertView); ) defines a type of block, called SIAlertViewHandler . 第一行( typedef void(^SIAlertViewHandler)(SIAlertView *alertView); )定义了一种称为SIAlertViewHandler的块类型。

The second line ( @property (nonatomic, copy) SIAlertViewHandler willShowHandler; ) defines a property which is going to store an instance of that block type. 第二行( @property (nonatomic, copy) SIAlertViewHandler willShowHandler; )定义了一个属性,该属性将存储该块类型的实例。

Yes, that is a block. 是的,那是一个障碍。

The first line is creating a new type to add a bit of sanity to using said block (instead of having to repeat void(^)(SIAlertView *alertView) everywhere, you can just use SIAlertViewHandler . 第一行是创建一种新类型,以增加使用该块的理智性(而不必在所有地方重复void(^)(SIAlertView *alertView) ,您只需使用SIAlertViewHandler

SIAlertView.m is checking to see if the showHandler is set and, if so, calling the block. SIAlertView.m正在检查是否设置了showHandler ,如果已设置,则调用该块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM