I'm trying to convert an Objective-C project to Swift, here is one of the .h file in Objective-C:
typedef void(^PrintBlock)(HLPrinter *printer);
@interface ShoppingViewController : UIViewController
@property (copy, nonatomic) PrintBlock printBlock;
@end
and in the .m file, there is a function:
HLPrinter *printer = [self getPrinter];
if (_printBlock) {
_printBlock(printer);
}
And this is how I converted it into Swift:
typealias PrintBlock = (_ printer: HLPrinter?) -> Void
But when I tried to convert the function above into Swift, I get a 'Non-nominal type 'PrintBlock' (aka '(Optional<HLPrinter>) -> ()') does not support explicit initialization'
error when I was trying to declare a variable with type PrintBlock
:
let pb = PrintBlock()
I'm not familiar with Objective-C, so how should I convert that function with _printerBlock
into Swift?
PrintBlock
is an alias for a closure. You can't simply create an instance like you would a class or struct. You need to assign a closure (or function) to the property.
The Swift code would be:
typealias PrintBlock = (_ printer: HLPrinter?) -> Void
class ShoppingViewController: UIViewController {
var printBlock: PrintBlock?
func getPrinter() -> HLPrinter {
return somePrinter
}
func someFunction() {
if let printBlock = printBlock {
let printer = getPrinter()
printBlock(printer)
}
}
}
The calling code would be something like:
let vc = ShoppingViewController()
vc.printBlock = { (printer) in
// do something with printer
}
Or if you have a function:
func someFunction(_ printer: HLPrinter) {
}
You can assign that:
vc.printBlock = someFunction
There's a few assumptions in here based on the partial information you have provided in your question.
You can not initialise the Closure , For you requirement you can simply create variable just like property in objc
typealias Listener = (HLPrinter) -> ()
var lister:Listener?
And on some event
self.lister?(value)
Hope it is helpful