Is there a way to simplify typing Swift trailing c

2020-03-26 04:03发布

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?

标签: xcode swift
2条回答
够拽才男人
2楼-- · 2020-03-26 04:22

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.

查看更多
Explosion°爆炸
3楼-- · 2020-03-26 04:25

If the closure takes parameters, you just do:

actionButton.snp_makeConstraints { make in 
    // Stuff
}

Multiple params:

actionButton.snp_makeConstraints { make, extraParam in
    // Stuff
} 

No params:

actionButton.snp_makeConstraints {
    // Stuff
}

For future reference: http://goshdarnclosuresyntax.com

查看更多
登录 后发表回答