I think that all of us who write on
Swift, even occasionally, used such a construction of the language as blocks. But there is one nuance with these blocks, which is very annoying: in Xcode, the formatting of the code does not quite work correctly with respect to them, or rather, indentation suffers.
They suffer if the block is multiline and is specified as one of the parameters when the method is called, and the code is formatted line by line, that is, the parameter is indicated on a separate line.
In this case, the simplest and most typical formatting works well:
DispatchQueue.main.async {
But it is worth transferring the indication of the parameter to a separate line, as the disco begins:
')
DispatchQueue.main.async( execute: {
However, if you add a seizure, then everything works naura:
DispatchQueue.main.async( execute: { [weak self] in // ok } ) DispatchQueue.main.async( execute: { [unowned self] in // ok } )
I wrote about the problem at Apple, and it seems that they have long been aware of it.
Here I have to use the capture, just to keep the formatting.
Unfortunately, besides solving the main problem, this method creates another problem -
warnings that
self is not used inside the block. But personally, I
find it easier to come to terms with such
warnings , than to observe downed indents.
If someone did not know and someone the note would be useful, then good.
If someone knows how the described problem can be solved more concisely, I will be glad to know.