Search code examples
swiftxcodecode-snippets

Xcode default snippets (if, for, struct etc.) not showing in autocomplete


I am new to Swift and also Xcode. In many tutorials I can see that snippets are really usefull especially for the beginners like me. Unfortunately I cant use them. When I am typing something, for example iforfor, nothing shows up in the autocomplete section. Or if it does (like for struct`) when I hit enter it just autocompletes my word.

I want it to look like this:

enter image description here

Instead, it just autocompletes:

enter image description here

if, for, etc. doen't even show up in autocomplete.

enter image description here

I couldn`t find a solution. Any help would be really useful.

I basicly want it to work like this https://sarunw.com/posts/how-to-create-code-snippets-in-xcode/#using-xcode-snippets


Solution

  • First, make sure the proper settings are enabled in Xcode. Go to Xcode -> Preferences (or Settings depending on your version of macOS). Then select the "Text Editing" tab. Then select the "Editing" sub-tab. Ensure the "Suggest completions while typing" option is enabled.


    While typing in Xcode, begin typing the struct keyword but don't press return. You should see the following:

    enter image description here

    By selecting the "struct - Struct" choice from the menu, you will end up with desired code block snippet.

    This also works when you want to add a code snippet for an if statement. Begin by typing if (make sure you are entering this in a valid context for an if block). Xcode will display appropriate choices:

    enter image description here

    Selecting, for example, the "if - If Statement" choice from the menu will leave you with an appropriate snippet:

    enter image description here

    Similar snippets are available for various statements such as for and while, etc.