Beginner question re: struct init parameters

I've been an amateur programmer for years and have coded in C, VBA, Python, and Objective C. I've been learning Swift and I am stuck on a concept that can be illustrated by the Apple documentation on the AsyncImage Struct. Here is an excerpt from their documentation:

You can specify a custom placeholder using init(url:scale:content:placeholder:). With this initializer, you can also use the content parameter to manipulate the loaded image. For example, you can add a modifier to make the loaded image resizable:

AsyncImage(url: URL(string: "https://example.com/icon.png")) { image in
    image.resizable()
} placeholder: {
    ProgressView()
}
.frame(width: 50, height: 50)

The 'url' initialization parameter is passed in the way I would expect, inside the parentheses. However, the 'content' and 'placeholder' parameters are following the parentheses, which I am having trouble understanding. Is the code with the {..} being passed to the initializer like a parameter? Why is it outside he parentheses? Why is 'placeholder' named, but 'content' isn't? I've tried to search for resources to explain this, but have come up empty. I appreciate any help to point me in the right direction.

Thanks,
Erik

What you’re seeing here are called trailing closures.

If you need to pass a closure expression to a function as the function’s final argument and the closure expression is long, it can be useful to write it as a trailing closure instead. You write a trailing closure after the function call’s parentheses, even though the trailing closure is still an argument to the function.

3 Likes

That’s perfect, thank you!