Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to simplify typing Swift trailing closure?

Tags:

xcode

swift

Trailing closure make code more readable, but the coding process is not enjoyable at all. For example, when I want to type this kind of method (shown below) that only takes one closure parameter. There is no Code hint for choosing trailing closure:

enter image description here

I have to delete content inside(), and type the complete closure like that :

enter image description here

Is there a way to make this easier?

like image 751
duan Avatar asked Oct 08 '15 15:10

duan


People also ask

What is a trailing closure in Swift?

If the last parameter to a function is a closure, Swift lets you use special syntax called trailing closure syntax. Rather than pass in your closure as a parameter, you pass it directly after the function inside braces.

How many types of closures are there in Swift?

As shown in the above table, there are three types of closures in Swift, namely global functions, nested functions, and closure expressions. They differ in several aspects, including their use scopes, their names, and whether they capture values, which will be discussed more in a later section.

What is Clouser in Swift?

In Swift, a closure is a special type of function without the function name. For example, { print("Hello World") } Here, we have created a closure that prints Hello World . Before you learn about closures, make sure to know about Swift Functions.

Are closures important in Swift?

Closures can capture and store references to any constants and variables from the context in which they're defined. This is known as closing over those constants and variables. Swift handles all of the memory management of capturing for you.


1 Answers

Type ENTER twice, once to complete the method name, a second time to automatically format the closure.

When I begin to type the function name:

enter image description here

After hitting ENTER once:

enter image description here

Then, after hitting ENTER a second time:

enter image description here

To sum up: type the first letters of the function, wait for auto-suggest, then hit ENTER then ENTER again.

like image 179
Eric Aya Avatar answered Nov 03 '22 16:11

Eric Aya