Does "let _ = ..." (let underscore equal) have any use in Swift?

You will get a compiler warning if the method has been marked with a warn_unused_result from the developer documentation:

Apply this attribute to a method or function declaration to have the compiler emit a warning when the method or function is called without using its result.

You can use this attribute to provide a warning message about incorrect usage of a nonmutating method that has a mutating counterpart.


Using let _ = ... specifically tells the compiler that you know that the expression on the right returns a value but that you don't care about it.

In instances where the method has been marked with warn_unused_result, if you don't use the underscore then the compiler will complain with a warning. (Because in some cases it could be an error to not use the return value.)

Tags:

Ios

Swift