将Objective-C转换为Swift以获取以下代码

时间:2018-04-20 10:19:53

标签: objective-c swift converter

我尝试使用在线转换器将此Objective C代码转换为Swift-4,但有些人未能做到这一点,

typedef void (^ActionBlock)();

## interface of custom class ##

@interface UIBlockButton : UIButton {
     ActionBlock _actionBlock;
}

## implementation of custom class ##

@implementation UIBlockButton

    -(void) handleControlEvent:(UIControlEvents)event
             withBlock:(ActionBlock) action {

        _actionBlock = Block_copy(action);
        [self addTarget:self action:@selector(callActionBlock:) forControlEvents:event];

    }

    -(void) callActionBlock:(id)sender{
        _actionBlock();
    }

    -(void) dealloc{
        Block_release(_actionBlock);
        [super dealloc];
    }

@end

这是swift代码的输出 ::: https://ibb.co/fRZFP7

1 个答案:

答案 0 :(得分:1)

试试这个

typealias ActionBlock = () -> Void

class UIBlackButton: UIButton {

    var actionBlock: ActionBlock = {}

    func handleControlEvent(event: UIControlEvents, action:  @escaping ActionBlock) {
        actionBlock = action
        self.addTarget(self, action: #selector(callActionBlock), for: event)
    }

    @objc func callActionBlock(sender: Any) {
        actionBlock();
    }  
}