0

I want to have function as variable in function declaration and than call that variable within declared function. How to do it in Swift?

Pseudo code:

let something = 0

func one() {
    print("one")
}

// Definition
func two( funcVariable: Void, number: Int) {
   print("\(number)")
   funcVariable() // here I want to call variable function 
}

// Call
two(funcVariable: one(), number: somethhing)

How to do it?

Example code appreciated.

Anjali Shah
  • 720
  • 7
  • 21
Win Fan
  • 77
  • 11

1 Answers1

1

Here is how you do it:

let something = 0

func one() {
    print("one")
}

// Definition
func two(funcVariable: () -> Void, number: Int) {
   print("\(number)")
   funcVariable()
}

// Call
two(funcVariable: one, number: something)
Frankenstein
  • 15,732
  • 4
  • 22
  • 47