1

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?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Compound07
  • 143
  • 2
  • 8

2 Answers2

2

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.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
0

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

Prashant Tukadiya
  • 15,838
  • 4
  • 62
  • 98