1

I have a function called update that looks like this:

func update(animated: Bool, completion: @escaping (Bool) -> ()) {

}

Now I want to queue multiple calls to this function in an array. How can I do this?

I need an array with a type:

var queue = [???]()

and to append the function like queue.append(???), but I can't figure it out.

Tometoyou
  • 7,792
  • 12
  • 62
  • 108

2 Answers2

2

Literally just solved my own question after posting this. The answer is this:

private var queue = [(Bool, (Bool) -> ()) -> ()]()

and queue.append(update)

Tometoyou
  • 7,792
  • 12
  • 62
  • 108
2

To simplify how you could achieve it, just copy your function signature:

(animated: Bool, completion: @escaping (Bool) -> ())

and use it for declaring a typealias:

typealias Update = (animated: Bool, completion: (Bool) -> ())

You'll see syntax error as:

@escaping attribute may only be used in function parameter position

with a suggestion with removing the @escaping, do it:

typealias Update = (animated: Bool, completion: (Bool) -> ())

At this point, you will be able to recognize the appropriate type by using the Update typealias.

Therefore, you are able to declare your queue array as:

var queue = [Update]()

Note that when declaring a typealias you could just avoid naming the parameters, as:

// this is sufficient
typealias Update = (Bool, (Bool) -> ())

Furthermore:

For knowing what typealias is, you could check: When to use typealias? (as well as the story mentioned in its answer)

Ahmad F
  • 30,560
  • 17
  • 97
  • 143