对于以下代码,将 Objective-C 转换为 Swift
Convert Objective-C to Swift for these below code
我尝试使用在线转换器将此 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
试试这个
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();
}
}
我尝试使用在线转换器将此 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
试试这个
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();
}
}