In the screenshot, I'm creating a static DateFormatter. I've forgotten to call the closure by including "()" after the closing brace.
I could have SWORN that Xcode/Swift previously had a very nice error message here that said something to effect of, "Did you mean to call the closure with ()?" I know it has saved me in the past.
Now, the error message is relatively ambiguous and the proposed fix is nonsense. This is Xcode 12.5 (12E262). Before I file a bug, has anyone else noticed a regression in this area?
The solution offered by Xcode when you click the Fix button is:
-static var newNameDateFormatter: DateFormatter = {
+static var newNameDateFormatter: DateFormatter = { () -> DateFormatter in
(This is only necessary for "complex" multiline closures.)
Then the next error message is:
error: function produces expected type 'DateFormatter'; did you mean to call it with '()'?
I don't get the "did you mean to call it with '()'?" error message for a static variable, but I DO get it for a lazy variable. You're seeing that error message with 'static' AND 'lazy'?
xedin
(Pavel Yaskevich)
4
The reason why calling is suggested is due to the presence of = in that declaration because it makes compiler think that what follows after = is an initialization value, that's why it correctly suggests to call the closure to initialize the newNameDateFormatter variable. Did you mean to declare a getter instead in this case?