Question: Initializer of class `A` takes an optional closure as argument:

```
class A {
   var closure: ()?

   init(closure: closure()?) {
      self.closure = closure
      self.closure()
   }
}

```

I want to pass a function with an argument as the closure:

```
class B {
    let a = A(closure: action(1)) // This throws the error: Cannot convert value of type '()' to expected argument type '(() -> Void)?'

    func action(_ i: Int) {
       //...
    }
}

```

Class `A` should execute the closure `action` with argument `i`.

I am not sure about how to write this correctly, see error in code comment above. What has to be changed?

Answer: 
Please make your "what-you-have-now" code error free.

Assuming your class `A` like this:

```
class A {
    typealias ClosureType = ()->Void

    var closure: ClosureType?

    init(closure: ClosureType?) {
        self.closure = closure
        //`closure` would be used later.
    }

    //To use the closure in class A
    func someMethod() {
        //call the closure
        self.closure?()
    }
}

```

With `A` given above, you need to rewrite your class `B` as:

```
class B {
    private(set) var a: A!
    init() {
        //initialize all instance properties till here
        a = A(closure: {[weak self] in self?.action(1)})
    }

    func action(i: Int) {
        //...
    }
}

```
One way to do what I think you're attempting is with the following code: 

```
class ViewController: UIViewController {

    override func viewDidLoad() {
        let _  = A.init(){Void in self.action(2)}
    }

    func action(i: Int) {
        print(i)
    }
}

class A: NSObject {
    var closure : ()?

    init(closure: (()->Void)? = nil) {
        // Notice how this is executed before the  closure
        print("1")
        // Make sure closure isn't nil
        self.closure = closure?()
    }
}

```