Understanding Swift Trailing Closure Syntax

preview_player
Показать описание
In this video I want to help you understand trailing closures and swift's trailing closure syntax and to help you make sense of some of the different initializers for controls like buttons and lists in SwiftUI.

You often hear people talk about making your code more Swifty. What exactly does that mean? I suppose it means different things to different people, but really, what it means is to write idiomatic Swift and by this, I mean to write code that looks and feels like Swift including swift specific language constructs and conventions and to avoid void patterns that might be more ore common in other programming languages.

A big part of this is the trailing closure syntax that is used.

As swift evolves, the language changes. As recent as Swift 5.3, the introduction to multiple trailing closures made for a simpler way to call a function with several closures and this made for simpler, more readable constructors for things like buttons.
If you want to support my work, you can buy me a coffee :)

Table of Contents:
0:00 Introduction
1:57 Introduction to Closures
6:57 Trailing Closures
12:41 Multiple Trailing Closures
18:50 Understanding the Button Initializer
23:47 Lists and onDelete closure

Starter Project Files:
(If you are having problems downloading zip files, use Safari. Chrome restricts zip files by default)

Completed Project:

References mentioned in video:
Closures Video:

Other References:
Stewart Lynch's YouTube Channel:
Stewart Lynch on Mastodon:
Stewart Lynch on Twitter:
Stewart Lynch on LinkedIn
Рекомендации по теме
Комментарии
Автор

The many versions of the trailing closure syntax have given me many headaches when I began working with Swift. I would have loved to have the video then. Your videos that concentrate on a specific topic and/or best practices are both explainers and guidelines for the beginner and intermediate developper. Thanks for this great video. Please continue.

larschristiansen
Автор

It’s great how you step through all the iterations of approach, it really helps me as a learner to identify and understand all the variants I see in the wild.

TonyBurnUK
Автор

wish i had seen this years ago - the mystery of swift syntax is explained.

doctorstrangelove
Автор

Great video. Love the provided Playground for later reference.

DirkBarchmann
Автор

Excellent, exceptional and insightful. I always wondered the various syntax options between Button, List, Section, etc. This video fully demystified. There are many insightful moments in this video, I had to pause and rewind many times to get it. This really helped increase my ability to understand code and is helping me write better Swifty code. Thanks a lot, Stewart.

santhoshVnair
Автор

This could be the best video for trailing closures and I don’t get it why it is so little watched. Many thanks!

serhancoskun
Автор

Quite an interesting discussion on trailing closures. Thank you.

parietal
Автор

28:19 There is a gem starting at this location. Until 29:03.

gjermundification
Автор

excellent explanation of these concepts!

TimMitra
Автор

That's really helpful video.Thanks Stewart!

firatylmaz
Автор

How did Stewart in the end convince me that those SwiftUI wilds syntax are clever nested functions in form of closures.

emmanuelbyiringiro
Автор

Why did you remove video of closure’s? The link given in desc not working

shivajitanpure