Jill Clover
Jill Clover

Reputation: 2328

Anonymous Function without Types in Swift

I don't fully understand the following code. I can identify there is a trailing closure. I don't know why

  1. the function type is missing
  2. images is not a function.

but it's valid.

    self.getImages(request) { images in // this line I don't understand
        if let images = images {
            // do something

        } else {
            // do something
        }
    }

Upvotes: 1

Views: 125

Answers (1)

Marco Santarossa
Marco Santarossa

Reputation: 4066

You can read it like this:

 self.getImages(request, completion:{ images in // this line I don't understand
    if let images = images {
        // do something

    } else {
        // do something
    }
})

images is the parameter of your closure.

This syntax is called trailing closure, here documentation.

Upvotes: 1

Related Questions