Number of arguments in the method alert

I’m not sure if it’s an errata in the book (p.91, chapter 1) or just myself that misunderstand something. It says that alert() requires two arguments, a binding, and an object.

The attached code is :
.alert(isPresented: self.$alertIsVisible) { Alert(title: Text("Hello there!"), message: Text("This is my first pop-up."), dismissButton: .default(Text("Awesome!"))) }

However, I see one argument within the parenthesis (the binding), and then in the mini program between braces is the object. Am I wrong ?

Also, how would you read the first line in natural language ?
“The method alert … ?”

Thanks for your help and highlights !

You are correct. The syntax in this case is called “trailing closure syntax“. Because the last (or in this case second) argument to the method is a function (or, to use another term, a closure), you can close the parentheses after the first argument and add the implementation of that function after it in curly braces.
Closures are a more advanced topic and I’m not quite sure why the authors chose to throw it around at that early stage in the book.

1 Like

This topic was automatically closed after 166 days. New replies are no longer allowed.