|
Hi I'm re-reading Pauls' tutorial on Closures. https://www.hackingwithswift.com/quick-start/beginners/how-to-accept-functions-as-parameters I noticed that when calling the function, there is
I saw this happens also in projects's tutorial's codes. My question is : When to use Thanks in advance |
|
Only the first one uses just the opening and closing brace, all subsequent ones with use the paramater label followed by a colon. Taken from the Unwrap App, Trailing closure syntax
When the last parameter is a closure we can call travel() using trailing closure syntax. In other words we use {}. Then the following closures will be formated differentely, with the end brace from the previous closure, the external parameter name and a colon, and then another brace. https://www.hackingwithswift.com/quick-start/beginners/how-to-accept-functions-as-parameters
Example with a button:
|
|
Alert is another one that shows an example of this. There's probably tons more that I haven't learned yet as well.
|
|
Vince has the right answer. The syntax you see is the result of using multiple trailing closures. It's great that you see this and are asking questions. We see the gears working in your head to make sense of SwiftUI. Nice! Usually I might derive some car, or house-based example to help clarify this concept. But at this point in your development, you might want to know that when Swift was first released, trailing closure syntax was available, but only if the LAST parameter in a function's signature was itself a function. From the start, developers wanted more! Why not have multiple trailing closures? One of the ways that Swift evolves is for the Swift community to propose changes. These changes include detailed proposals and examples on how new syntax might work, and of course includes a wonderful essay on the many benefits to humanity. Other developers get to weigh in and give their thoughts as to why the proposal is complete rubbish, and would produce rabid code devils that would plague humans forever. Some proposals die a long and painful death. Your new friend Multiple Trailing Closures survived the ordeal! Take a look at this github repository for some of the juicy details and comments. |
|
@twostraws gets very exicited when Apple adds new capabilities to Swift and SwiftUI. So much so, he publishes entire videos explaining new features with references to the original Swift evolution proposals. Here's a link to his video on new stuff in Swift 5.5. Take a quick peek at the article. See the references to SE-0296 ? This is the original proposal for Async / Await syntax. |
|
I struggled with that too. Thanks for your question and also thanks for the answers. |
|
SPONSORED Optimize your app’s startup time, binary size, and overall performance using Emerge’s advanced app optimization and monitoring tools. Reliably measure app size, speed up your app's startup time with Emerge's Launch Booster, and much more. Emerge is actively used by many of the top mobile development teams in the world.
Sponsor Hacking with Swift and reach the world's largest Swift community!
You need to create an account or log in to reply.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.