9

In Xcode 10 code completion, the text underlying the placeholder tokens has an extra #T# before it (to see that this is so, copy and paste the inserted code template into a different text editor):

let alert = UIAlertController(
    title: <#T##String?#>, message: <#T##String?#>, 
    preferredStyle: <#T##UIAlertController.Style#>)

What is that? Does "T" mean Type? What difference does it make in my usage of the placeholder?

Ashish Kakkad
  • 23,586
  • 12
  • 103
  • 136
matt
  • 515,959
  • 87
  • 875
  • 1,141

1 Answers1

10

The syntax <#T##_A_##_B_#> is used to specify a default piece of code for the placeholder. The placeholder will be displayed as _A_, but when the user presses the enter key the placeholder will be replaced with _B_.

Placeholder:

enter image description here

After pressing enter:

enter image description here

It'd a useful feature when presenting something to an audience, because as opposed to snippets, I would't need to remember the name of each snippet, I'd just select a placeholder and press enter to get the right piece of code.

EDIT:

Answering your question, indeed it seems that the T refers to type. If you try to replace the placeholder with an expression, like <#T##Example1##let i = 3#>, the placeholder it's not replaced with let i = 3 as you would expect. It is instead replaced with <<error type>>.

Furthermore, this placeholder <#T##transform: (Error) throws -> U?##(Error) throws -> U?#> is replaced with:

{ (<#Error#>) -> U? in
    <#code#>
}

My guess is that when you prepend the T you are telling Xcode that you will provide a type, then Xcode finds an appropriate default value for that type.

  • I found no reference to this, I figured it out by copying some of the placeholders that appear with autocomplete. – ferranpujolcamins Jan 22 '20 at 09:39
  • the syntax is referenced in the [playground documentation](https://developer.apple.com/documentation/swift-playgrounds/specifying-editable-regions-in-a-playground-page) – sean woodward Aug 31 '23 at 17:06