iosswiftxcodexcode10code-completion

Xcode 10 code completion placeholders — what is the "T"?


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?


Solution

  • 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.