Please explain this SwiftUI code completion

Please end my confusion. Thanks! 🙂


Suppose, for purposes of illustration, I want to end up with this:


Button(showSheet ? "Hide message" : "Show message") {
                self.showSheet.toggle()
            }


How would I - step-by-step - use Xcode (Version 11.4.1) code completion toachieve this?


This is what is confusing me:


  1. I start typing "Button"; choose the suggested `Button` symbol and hit Enter to be reminded of the sequence of arguments for this View.
    I hit Enter and I am given "Button"
    I am not offered any argument hints
  2. I type "("
    In the expectation of being shown what arguments follow.
    I see
    Button(action: () -> Void, label: () -> PrimitiveButtonStyleConfiguration.Label
    Didn't I want to see the placeholders that relate to this initializer:
    init(S, action: () -> Void)


I suppose my confusion stems from the fact that a common form of initialization is not shown in code completion.

What am I getting wrong?

Replies

I tried it, with your sequence.

1. Type But

Offered to complete with Button

Enter to validate

2. Type (

Offered multiple options:

Button(PrimitiveButtonStyleConfiguration)

Button(action: <#T##() -> Void#>, label: <#T##() -> _#>)

Button(LocalizedStringKey, action: <#T##() -> Void#>)


I think the one you search for is the last:

Button("Test", action: test () )


Or the second, which corresponds to the following example


func test() {
     print("Nothing, just to test")
}


Button(action: test, label: { Text("Test") }  )


or with trailing closure

Button(action: test) { Text("Test") }


Note here we pass the func, ( test ) not the func complete signature ( test() )

That's really interesting because I am not offered those (sensible) completions. Just the one in my post plus one other


Button(<#T##configuration: PrimitiveButtonStyleConfiguration##PrimitiveButtonStyleConfiguration#>)


... plus a great many suggestions seemingly not to do with initializing a Button AFAICS

I raised an issue FB7679679